0

I am trying to upload an image file to a local folder and getting File Not Found Error. The location pointing out in error message for the upload file is not right. Actually the file location is showing on the error is a combination of my source and destination location. I am trying to upload file to "C:/Users/sam//File/Upload/" from my desktop. Here is the error code,

Error message

java.io.FileNotFoundException: C:\Users\sam\File\Upload\C:\Users\sam\Desktop\test.jpg (The filename, directory name, or volume label syntax is incorrect)

My class file

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;

@ManagedBean
public class FileUploadView {
private String fileUploadFolder = "C:/Users/sam/File/Upload/";

public void handleFileUpload(FileUploadEvent event) {
    FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, message);

    try {
        File targetFolder = new File(fileUploadFolder);
        InputStream input = event.getFile().getInputstream();
        OutputStream output = new FileOutputStream(new File(targetFolder,
                event.getFile().getFileName()));
        try {
            IOUtils.copy(input, output);
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  

}

}

and my html

    <p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}"
        mode="advanced" dragDropSupport="false" update="messages"
        sizeLimit="100000000" fileLimit="1"
        allowTypes="/(\.|\/)(gif|jpe?g|png|xlsx|jpg)$/" />

I also tried example from BalusC, still getting same error, ui with primefaces5.1.

Community
  • 1
  • 1
SamK
  • 377
  • 9
  • 27
  • Your example works for me as expected without any problems. I used "Mojarra 2.2.9" + Tomcat 7 + Primefaces 5.1. File copy successful. – pL4Gu33 Feb 24 '15 at 19:56
  • @ pL4Gu33-I know it is a weird issue, it was working before and now I start to get the problem. I have the same "Mojarra 2.2.9" + Tomcat 7 + Primefaces 5.1. Any other place you recommend to check? – SamK Feb 24 '15 at 20:23
  • I am wondering why I am getting the file location C:\Users\sam\File\Upload\C:\Users\sam\Desktop\test.jpg like this? Any typo that I can't figure out? – SamK Feb 24 '15 at 20:39
  • OK, I figured out that this issue is coming only with IE8 that I have in my system. In Forefox, it is working fine. Is this a bug from primefaces or jsf? – SamK Feb 24 '15 at 21:15
  • IE is not a real browser @KodS, IE8 even less so – kolossus Feb 24 '15 at 22:16
  • It's a securtity problem in IE8. Search the internet. Nothing to do with jsf or primefaces – Kukeltje Feb 24 '15 at 23:34

1 Answers1

3

As hinted by the clearly wrong file path in the exception,

java.io.FileNotFoundException: C:\Users\sam\File\Upload\C:\Users\sam\Desktop\test.jpg (The filename, directory name, or volume label syntax is incorrect)

the problem is the combination of this piece of code

OutputStream output = new FileOutputStream(new File(targetFolder,
    event.getFile().getFileName()));

and the fact that MSIE browser was being used. This browser has the security bug that it sends the entire client side disk file system path along with the file name instead of only the file name.

You basically need to get rid of that client side disk file system path from the file name.

String fileName = event.getFile().getFileName();
fileName = fileName.substring(fileName.lastIndexOf('\\') + 1); // MSIE fix.
OutputStream output = new FileOutputStream(new File(targetFolder, fileName));
// ...

You've however a bigger problem here. You seem to not have considered the possibility that multiple different users can upload a file having exactly the same filename. If you'd like to make your code more robust as to that, then head to How to save uploaded file in JSF.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • In the solution, still it is pointing to the same combined file path location. I believe instead of ('\\'), may need to go for (':'). Thank you for the Thank you for the detailed explanation and guidance, I can rewrite code based on this guidance. – SamK Feb 25 '15 at 18:39