1

This is the error I am getting:

java.io.FileNotFoundException: C:\Users\Owner\AppData\Roaming\NetBeans\7.3.1\config\GF3\domain1\generated\jsp\uploadRamki\data\images.jpg (The system cannot find the path specified)

This is my backing bean:

package beans;

import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.servlet.http.Part;

@Named(value = "demoBean")
@SessionScoped
public class DemoBean implements Serializable {

    private Part file1;

    public Part getFile1() {
        return file1;
    }

    public void setFile1(Part file1) {
        this.file1 = file1;
    }

    // getters and setters for file1 and file2
    public String upload() throws IOException {
        file1.write("c:/data/" + getFilename(file1));
        return "success";
    }

    private static String getFilename(Part part) {
        for (String cd : part.getHeader("content-disposition").split(";")) {
            if (cd.trim().startsWith("filename")) {
                String filename = cd.substring(cd.indexOf('=') + 1).trim()
                    .replace("\"", "");
                return filename.substring(filename.lastIndexOf('/') + 1)
                    .substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
            }
        }
        return null;
    }
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Dave
  • 11
  • 4
  • I got this code from this [web](http://www.ramkitech.com/2013/06/file-upload-is-easy-in-jsf22.html): http://www.ramkitech.com/2013/06/file-upload-is-easy-in-jsf22.html – Dave Oct 13 '13 at 13:13
  • Besides the fact that it's generally a bad idea to store application artifacts in a server-generated directory, I don't see where you're referencing that path in your code/config – kolossus Oct 13 '13 at 17:50
  • file1.write("/data/"+getFilename(file1)); This is where I'm referencing the folder which is located on my C:/data file on the disk. my intention is not to store the uploaded image on that location and then store the image path in the database. so that i can use it in my application. – Dave Oct 14 '13 at 04:39
  • @Kolossus file1.write("c:/data/"+getFilename(file1)); – Dave Oct 19 '13 at 08:18
  • Look, the point here is you should not be using the glassfish temp folders for anything. Seeing as you're using netbeans, you should also know that the glassfish installation folder in your C drive is not the same one being used by glassfish, it's going to be in your user profile foler (a folder called .netbeans). Create a folder under the Web Pages folder of your Netbeans folder. It's this folder you should be using to store files – kolossus Oct 20 '13 at 14:51
  • can you post the exception - all of it ? – Mr_and_Mrs_D Oct 31 '13 at 14:54

1 Answers1

0

If you do part.write it will try to write in the temp directory where glassfish is running.

Try:

        InputStream input = file1.getInputStream();
        FileOutputStream output = new FileOutputStream(URL_FILES + file1.getSubmittedFileName());
        byte[] buf = new byte[1024];
        int len;
        while ((len = input.read(buf)) > 0) {
            output.write(buf, 0, len);
        }
        input.close();
        output.close();
bruno
  • 21
  • 2