java.io.FileNotFoundException(文件或目录不存在) – 下载文件

huangapple go评论74阅读模式
英文:

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

问题

以下是您提供的代码的翻译部分:

上传功能的代码片段:

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", "文件上传成功");
        } else {
            request.setAttribute("gurumessage", "请使用约定的文件名");
        }
    }
}

下载功能的代码片段:

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();
英文:

I have Web Application hosted on Linux, contains page to upload .rar file and another page to download it. for upload function working fine and file uploaded successfully to server but for download it gives me below exception:

 [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)

I used below funcation to make upload:

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");

				}

and here function for download that i face issue on it and above exception appear:

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();

答案1

得分: 1

这个错误的唯一原因是文件在该路径下找不到。

请验证路径

    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")

应该可以解决问题。

英文:

The only reason for this error is that file cannot be found under that path.

Please verify the path

    String gurufile = &quot;QCAM.rar\\&quot;;
    String gurupath = &quot;\\usr\\local\\apache-tomcat-8.5.31\\OSS-CPE-Tracker\\Zaky&quot;;
    // &lt;...&gt;
    FileInputStream fileInputStream = new FileInputStream(gurupath
            + gurufile);

In unix systems file path is resolved using forward slash / and not a backslash \.

Try changing to the same value as your upload script:

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

That should do

huangapple
  • 本文由 发表于 2020年10月8日 03:19:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64250903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定