在Internet Explorer中运行时出现java.io.FileNotFoundException [重复](java.io.FileNotFoundException when running in Internet Explorer [duplicate])

这个问题在这里已有答案:

如何使用JSP / Servlet将文件上传到服务器? 12个答案

我正在使用servlet上传文件。 它在Chrome中运行良好,但在Internet Explorer中无法运行。

http://www.technicalkeeda.com/servlet-jsp-tutorials/how-to-upload-file-using-servlet-jsp

尝试获取文件后,我收到以下错误。

java.io.FileNotFoundException: C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\MultiPartFilterServlett.java (The filename, directory name, or volume label syntax is incorrect) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:221) at java.io.FileOutputStream.<init>(FileOutputStream.java:171) at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:417) at com.itws.itwsPost.doPost(itwsPost.java:194) at javax.servlet.http.HttpServlet.service(HttpServlet.java:208) at javax.servlet.http.HttpServlet.service(HttpServlet.java:306) at javax.servlet.http.HttpServlet.service(HttpServlet.java:333) at com.evermind._csb._pvd(Unknown Source) at com.evermind._csb._boc(Unknown Source) at com.evermind._ax._lsc(Unknown Source) at com.evermind._ax._uab(Unknown Source) at com.evermind._bf.run(Unknown Source)

它似乎是由正在设置的变量引起的。

这就是它在IE中的样子

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\Stored procedure executed from SQL server.docx

这就是Chrome中的样子

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\Combined SRS Sites.doc

这是它在查看文件时的作用。

if (fileName != "") {
  System.out.println("Getting file");

  String root = getServletContext().getRealPath("/");
  File path = new File(root + "/IT/uploads");

  System.out.println("Got path:" + path);

  if (!path.exists()) {
    boolean status = path.mkdirs();
  }

  File uploadedFile = new File(path + "/" + fileName);

  System.out.println("Set UploadedFile:" + uploadedFile);
  //File uploadedFile = new File(path + "/" + Title.replaceAll("\\s","") +"_"+ fileName);

  filePath = uploadedFile.getAbsolutePath();
  item.write(uploadedFile); // File has been uploaded to server
  System.out.println("File Uploaded:" + uploadedFile);
  fl[loopCount] = filePath.toString();

} else {
  //System.out.print("No file has been detected, clearing out variable");
  filePath = null;
}
loopCount++; 
  
 

This question already has an answer here:

How to upload files to server using JSP/Servlet? 12 answers

I am using a servlet to upload a file. It works fine in Chrome however it will not work in Internet Explorer.

http://www.technicalkeeda.com/servlet-jsp-tutorials/how-to-upload-file-using-servlet-jsp

I receive the following error once it tries to get the file.

java.io.FileNotFoundException: C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\MultiPartFilterServlett.java (The filename, directory name, or volume label syntax is incorrect) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:221) at java.io.FileOutputStream.<init>(FileOutputStream.java:171) at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:417) at com.itws.itwsPost.doPost(itwsPost.java:194) at javax.servlet.http.HttpServlet.service(HttpServlet.java:208) at javax.servlet.http.HttpServlet.service(HttpServlet.java:306) at javax.servlet.http.HttpServlet.service(HttpServlet.java:333) at com.evermind._csb._pvd(Unknown Source) at com.evermind._csb._boc(Unknown Source) at com.evermind._ax._lsc(Unknown Source) at com.evermind._ax._uab(Unknown Source) at com.evermind._bf.run(Unknown Source)

It appears to be caused by a variable that is being set.

This is what it looks like in IE

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\Stored procedure executed from SQL server.docx

And this is what it looks like in Chrome

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\Combined SRS Sites.doc

Here is what it does when it sees a file.

if (fileName != "") {
  System.out.println("Getting file");

  String root = getServletContext().getRealPath("/");
  File path = new File(root + "/IT/uploads");

  System.out.println("Got path:" + path);

  if (!path.exists()) {
    boolean status = path.mkdirs();
  }

  File uploadedFile = new File(path + "/" + fileName);

  System.out.println("Set UploadedFile:" + uploadedFile);
  //File uploadedFile = new File(path + "/" + Title.replaceAll("\\s","") +"_"+ fileName);

  filePath = uploadedFile.getAbsolutePath();
  item.write(uploadedFile); // File has been uploaded to server
  System.out.println("File Uploaded:" + uploadedFile);
  fl[loopCount] = filePath.toString();

} else {
  //System.out.print("No file has been detected, clearing out variable");
  filePath = null;
}
loopCount++; 
  
 

最满意答案

这是因为IE正在发送完整路径,而不仅仅是文件名。 我添加了一些代码来从路径中获取名称并且它有效。 仍然可以在Chrome中使用。

itemName = item.getName();
String fileName = new File(itemName).getName(); // Removes the path if imported from IE. Chrome was not affected by this. 
  
 

It was because IE was sending the full path, not just the file name. I added some code to just get the name from the path and it worked. Still works in Chrome too.

itemName = item.getName();
String fileName = new File(itemName).getName(); // Removes the path if imported from IE. Chrome was not affected by this. 
  
 

在Internet Explorer中运行时出现java.io.FileNotFoundException [重复](java.io.FileNotFoundException when running in Internet Explorer [duplicate])

这个问题在这里已有答案:

如何使用JSP / Servlet将文件上传到服务器? 12个答案

我正在使用servlet上传文件。 它在Chrome中运行良好,但在Internet Explorer中无法运行。

http://www.technicalkeeda.com/servlet-jsp-tutorials/how-to-upload-file-using-servlet-jsp

尝试获取文件后,我收到以下错误。

java.io.FileNotFoundException: C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\MultiPartFilterServlett.java (The filename, directory name, or volume label syntax is incorrect) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:221) at java.io.FileOutputStream.<init>(FileOutputStream.java:171) at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:417) at com.itws.itwsPost.doPost(itwsPost.java:194) at javax.servlet.http.HttpServlet.service(HttpServlet.java:208) at javax.servlet.http.HttpServlet.service(HttpServlet.java:306) at javax.servlet.http.HttpServlet.service(HttpServlet.java:333) at com.evermind._csb._pvd(Unknown Source) at com.evermind._csb._boc(Unknown Source) at com.evermind._ax._lsc(Unknown Source) at com.evermind._ax._uab(Unknown Source) at com.evermind._bf.run(Unknown Source)

它似乎是由正在设置的变量引起的。

这就是它在IE中的样子

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\Stored procedure executed from SQL server.docx

这就是Chrome中的样子

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\Combined SRS Sites.doc

这是它在查看文件时的作用。

if (fileName != "") {
  System.out.println("Getting file");

  String root = getServletContext().getRealPath("/");
  File path = new File(root + "/IT/uploads");

  System.out.println("Got path:" + path);

  if (!path.exists()) {
    boolean status = path.mkdirs();
  }

  File uploadedFile = new File(path + "/" + fileName);

  System.out.println("Set UploadedFile:" + uploadedFile);
  //File uploadedFile = new File(path + "/" + Title.replaceAll("\\s","") +"_"+ fileName);

  filePath = uploadedFile.getAbsolutePath();
  item.write(uploadedFile); // File has been uploaded to server
  System.out.println("File Uploaded:" + uploadedFile);
  fl[loopCount] = filePath.toString();

} else {
  //System.out.print("No file has been detected, clearing out variable");
  filePath = null;
}
loopCount++; 
  
 

This question already has an answer here:

How to upload files to server using JSP/Servlet? 12 answers

I am using a servlet to upload a file. It works fine in Chrome however it will not work in Internet Explorer.

http://www.technicalkeeda.com/servlet-jsp-tutorials/how-to-upload-file-using-servlet-jsp

I receive the following error once it tries to get the file.

java.io.FileNotFoundException: C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\MultiPartFilterServlett.java (The filename, directory name, or volume label syntax is incorrect) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:221) at java.io.FileOutputStream.<init>(FileOutputStream.java:171) at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:417) at com.itws.itwsPost.doPost(itwsPost.java:194) at javax.servlet.http.HttpServlet.service(HttpServlet.java:208) at javax.servlet.http.HttpServlet.service(HttpServlet.java:306) at javax.servlet.http.HttpServlet.service(HttpServlet.java:333) at com.evermind._csb._pvd(Unknown Source) at com.evermind._csb._boc(Unknown Source) at com.evermind._ax._lsc(Unknown Source) at com.evermind._ax._uab(Unknown Source) at com.evermind._bf.run(Unknown Source)

It appears to be caused by a variable that is being set.

This is what it looks like in IE

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\C:\Users\testUser\Desktop\Stored procedure executed from SQL server.docx

And this is what it looks like in Chrome

Got path:C:\Orion\default-web-app\IT\uploads Set UploadedFile:C:\Orion\default-web-app\IT\uploads\Combined SRS Sites.doc

Here is what it does when it sees a file.

if (fileName != "") {
  System.out.println("Getting file");

  String root = getServletContext().getRealPath("/");
  File path = new File(root + "/IT/uploads");

  System.out.println("Got path:" + path);

  if (!path.exists()) {
    boolean status = path.mkdirs();
  }

  File uploadedFile = new File(path + "/" + fileName);

  System.out.println("Set UploadedFile:" + uploadedFile);
  //File uploadedFile = new File(path + "/" + Title.replaceAll("\\s","") +"_"+ fileName);

  filePath = uploadedFile.getAbsolutePath();
  item.write(uploadedFile); // File has been uploaded to server
  System.out.println("File Uploaded:" + uploadedFile);
  fl[loopCount] = filePath.toString();

} else {
  //System.out.print("No file has been detected, clearing out variable");
  filePath = null;
}
loopCount++; 
  
 

最满意答案

这是因为IE正在发送完整路径,而不仅仅是文件名。 我添加了一些代码来从路径中获取名称并且它有效。 仍然可以在Chrome中使用。

itemName = item.getName();
String fileName = new File(itemName).getName(); // Removes the path if imported from IE. Chrome was not affected by this. 
  
 

It was because IE was sending the full path, not just the file name. I added some code to just get the name from the path and it worked. Still works in Chrome too.

itemName = item.getName();
String fileName = new File(itemName).getName(); // Removes the path if imported from IE. Chrome was not affected by this.