java.io.FileNotFoundException(无此类文件或目录)-下载文件
我有一个在Linux上托管的Web应用程序,包含上载.rar文件的页面和另一个下载页面。上传功能正常,文件成功上传至服务器,但下载时出现以下异常:java.io.FileNotFoundException(无此类文件或目录)-下载文件,java,linux,Java,Linux,我有一个在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\Q
[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\\”;
字符串gurupath=“\\usr\\local\\apache-tomcat-8.5.31\\OSS CPE Tracker\\Zaky”;
//
FileInputStream FileInputStream=新的FileInputStream(gurupath
+古鲁菲尔);
在unix
系统中,使用正斜杠/
而不是反斜杠\
解析文件路径
尝试更改为与上载脚本相同的值:
FileInputStream FileInputStream=newfileinputstream(“/usr/local/apache-tomcat-8.5.31/OSS-CPE-Tracker/Zaky/QCAM.rar”)
这应该可以String gurufile=“QCAM.rar\\”代码>。。。也许这应该是String gurufile=“QCAM.rar”代码>