DEV Community

Lalit Kumar
Lalit Kumar

Posted on

The system cannot find the file specified java

In this article, we will discuss a common issue.When you face the following error: "java.io.FileNotFoundException: the system cannot find the file specified" and error similar to below is visible in logs:

  1. Exception in thread "main" java.io.FileNotFoundException: Report.PDF (The system cannot find the file specified)

  2. at java.io.FileInputStream.open(Native Method)

  3. at java.io.FileInputStream.(Unknown Source)

  4. at java.util.Scanner.(Unknown Source)

The issue as the error suggests is that system is not able to find the file; you need to provide the correct path for it. For example, a file can be either read on a relative path like
Report

Here APP_NAME is the name of your application that contains uploads directory which contains Report.PDF. This will open a file in the browser's new tab as a PDF document.


title: "originally posted here πŸ‘‡"

canonical_url: https://kodlogs.com/blog/2129/the-system-cannot-find-the-file-specified-java

Please note its not a good idea to store files inside the application, as once the application is reinstalled previous files are gone. Hence you can store files outside the application and files can be rendered from there. You can store the name of the file in a 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 the following link to download file dynamically using JSP:

  1. Report

download.jsp should have the following code, once the above link is clicked it will be displayed as an attachment box to the end-user which he/she can either Save or Open.

**1.

  1. <%

  2. String fileName = request.getParameter("fileName");

  3. response.setContentType("application/octet-stream");

  4. response.setHeader("Content-Disposition", "attachment;filename="+extractFileName(fileName));

  5. try {

  6. // You can set java.io.tmpdir in System property as "D://APP_NAME/uploads/" as its not a good idea to store files inside the application, as once the application is reinstalled previous files are gone.

  7. String filePath= System.getProperty("java.io.tmpdir");

  8. if(filePath==null || filePath.isEmpty()){
    
  9. %> Error: Please set java.io.tmpdir. <%

  10. }else{

  11. File file = new File(filePath+fileName); //"D://APP_NAME/uploads/Report.pdf"

  12. FileInputStream fileIn = new FileInputStream(file);

  13. ServletOutputStream output = response.getOutputStream();

  14. int len = fileIn.available();

  15. byte[] outputByte = new byte[len];

  16. //copy binary contect to output stream

  17. while(fileIn.read(outputByte, 0, len) != -1) {
    
  18. output.write(outputByte, 0, len);

  19. }

  20. response.setContentLength((int) file.length());

  21. fileIn.close();

  22. output.flush();

  23. output.close();

  24. }

  25. } catch (Exception e) {

  26. %> Exception ::: <%= e.getMessage() %> <%

  27. } %>

  28. <%!

  29. public String extractFileName( String filePathName ){

  30. if ( filePathName == null )  return null;
    
  31. int slashPos = filePathName.lastIndexOf( '\' );

  32. if ( slashPos == -1 ) slashPos = filePathName.lastIndexOf( '/' );
    
  33. return filePathName.substring( slashPos > 0 ? slashPos + 1 : 0 );
    
  34. }

  35. %>**

Read more in original post

Top comments (0)