If you face following error: "java.io.FileNotFoundException: the system cannot find the file specified" and error similar to below is visible in logs:
Exception in thread "main" java.io.FileNotFoundException: Report.PDF (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
Issue as the error suggest is that system is not able to find the file; you need to provide correct path for it. For example: file can be either read on relative path like
<a target='_blank' href='/APP_NAME/uploads/Report.PDF'>Report</a>
Here APP_NAME is name of your application which contain uploads directory which contain Report.PDF. This will open as file in browser new tab as PDF document.
Please note its not good idea to store files inside application, as once application is reinstalled previous files are gone. Hence you can store files outside application and files can be rendered from there. You can store name of file in database like Report.PDF and can set upload directory in System property i.e. java.io.tmpdir=D://APP_NAME/uploads/
For example you can create following link to download file dynamically using JSP:
<a href="download.jsp?fileName=Report.PDF">Report</a>
download.jsp should have following code, once above link is clicked it will be displayed as attachment box to end user which he/she can either Save or Open.
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="com.siperian.sif.message.Record"%>
<%@page import="com.siperian.lookup.processor.FindTaskProcessor"%>
<%
String fileName = request.getParameter("fileName");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+extractFileName(fileName));
try {
// You can set java.io.tmpdir in System property as "D://APP_NAME/uploads/" as its not good idea to store files inside application, as once application is reinstalled previous files are gone.
String filePath= System.getProperty("java.io.tmpdir");
if(filePath==null || filePath.isEmpty()){
%> Error: Please set java.io.tmpdir. <%
}else{
File file = new File(filePath+fileName); //"D://APP_NAME/uploads/Report.pdf"
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream output = response.getOutputStream();
int len = fileIn.available();
byte[] outputByte = new byte[len];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, len) != -1) {
output.write(outputByte, 0, len);
}
response.setContentLength((int) file.length());
fileIn.close();
output.flush();
output.close();
}
} catch (Exception e) {
%> Exception ::: <%= e.getMessage() %> <%
} %>
<%!
public String extractFileName( String filePathName ){
if ( filePathName == null ) return null;
int slashPos = filePathName.lastIndexOf( '\\' );
if ( slashPos == -1 ) slashPos = filePathName.lastIndexOf( '/' );
return filePathName.substring( slashPos > 0 ? slashPos + 1 : 0 );
}
%>
0 Comment(s)