0

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++;
Vicky
  • 9,515
  • 16
  • 71
  • 88
user3183411
  • 315
  • 2
  • 7
  • 19
  • How are you retrieving the file? How are you sending the file? Post some code to go with your question... – brso05 Jul 08 '15 at 18:48

1 Answers1

0

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.
user3183411
  • 315
  • 2
  • 7
  • 19