Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • the system cannot find the file specified

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 19.8k
    Comment on it

    If you face following error: " the system cannot find the file specified" and error similar to below is visible in logs:

    Exception in thread "main" Report.PDF (The system cannot find the file specified)
        at Method)
        at<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.

    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=""%>
    <%@page import=""%>
    <%@page import="com.siperian.sif.message.Record"%>
    <%@page import="com.siperian.lookup.processor.FindTaskProcessor"%>
     String fileName = request.getParameter("fileName");
     response.setHeader("Content-Disposition", "attachment;filename="+extractFileName(fileName));
     try {	 
    	 // You can set 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("");
    	 if(filePath==null || filePath.isEmpty()){
    		%> Error: Please set <%
    	 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(, 0, len) != -1) {
    		 output.write(outputByte, 0, len);
    	 response.setContentLength((int) file.length());
     } 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)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: