簡體   English   中英

java.io.FileNotFoundException(沒有這樣的文件或目錄) - 下載文件

[英]java.io.FileNotFoundException (No such file or directory) - Download File

我在 Linux 上托管了 Web 應用程序,包含上傳 .rar 文件的頁面和下載它的另一個頁面。 對於上傳功能工作正常並且文件成功上傳到服務器但對於下載它給我以下異常:

 [servelt.scriptdownloadservelt] in context with path [/OSS-CPE-Tracker] threw exception
  java.io.FileNotFoundException: \usr\local\apache-tomcat-8.5.31\OSS-CPE-Tracker\Zaky\QCAM.rar (No such file or directory)

我使用以下功能進行上傳:

String destDir = "/usr/local/apache-tomcat-8.5.31/OSS-CPE-Tracker/Zaky";
            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String name = new File(item.getName()).getName();
                    if(name.equalsIgnoreCase("QCAM.rar")) {
                                                
                    File destFile = new File(destDir, "QCAM.rar");
                    if (destFile.exists()) {
                        destFile.delete();
                    }
                    item.write(new File("/usr/local/apache-tomcat-8.5.31/OSS-CPE-Tracker/Zaky" + File.separator + name));
                    request.setAttribute("gurumessage", "File Uploaded Successfully");

                }else {
                    request.setAttribute("gurumessage", "Kindly use the agreed name");

                }

和這里的下載功能,我在上面遇到問題並且出現上述異常:

response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String gurufile = "QCAM.rar\\";
    String gurupath = "\\usr\\local\\apache-tomcat-8.5.31\\OSS-CPE-Tracker\\Zaky";

    
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=\""
            + gurufile + "\"");

    FileInputStream fileInputStream = new FileInputStream(gurupath
            + gurufile);

    int i;
    while ((i = fileInputStream.read()) != -1) {
        out.write(i);
    }
    fileInputStream.close();
    out.close();

此錯誤的唯一原因是在該路徑下找不到文件。

請驗證路徑

    String gurufile = "QCAM.rar\\";
    String gurupath = "\\usr\\local\\apache-tomcat-8.5.31\\OSS-CPE-Tracker\\Zaky";
    // <...>
    FileInputStream fileInputStream = new FileInputStream(gurupath
            + gurufile);

unix系統中,文件路徑使用正斜杠/而不是反斜杠\\解析。

嘗試更改為與upload腳本相同的值:

    FileInputStream fileInputStream = new FileInputStream("/usr/local/apache-tomcat-8.5.31/OSS-CPE-Tracker/Zaky/QCAM.rar")

那應該做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM