0

I am uploading a file with the PF 3.5 File Uploader

My Upload Method looks like that:

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

    InputStream inputStream = null;
    OutputStream out = null;

    try {
        File targetFolder = new File("\\resources\\uploads");
        if(!targetFolder.exists()) {
            targetFolder.mkdirs();
        }

        inputStream = event.getFile().getInputstream();
        File outFile = new File(targetFolder, event.getFile().getFileName());
        log.info("copy file stream to " + outFile.getAbsolutePath());
        out = new FileOutputStream(outFile);
        int read = 0;
        byte[] bytes = new byte[size];
        log.info("read file stream");           
        while ((read = inputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

        out.flush();
    } catch (IOException e) {
        log.error(e);
    } finally {
        ...
    }

at the moment my files get uploaded to \\resources\\uploads". Thats the path to a folder on theC:`.

However, I want to upload my uploads to a path in my eclipse project. How to change the path? I really appreciate your answer!!!

maximus
  • 11,264
  • 30
  • 93
  • 124

1 Answers1

4

However, I want to upload my uploads to a path in my eclipse project.

That's absolutely not recommended for the reasons mentioned in this answer: Uploaded image only available after refreshing the page. The point is: the IDE's workspace and server's deploy folder is absolutely not intented as a permanent file storage. The uploaded files would be unreachable and/or disappear like by magic.

Just keep them in a path external to the IDE's workspace and server's deploy folder. You're doing it fine. I'd only make the path configurable by a system property, environment variable or properties file setting so that you don't need to edit, recompile, rebuild, etc the code everytime when you change the upload location.

If your concrete problem is more the serving of the uploaded file, then just add the upload folder as another context in server's configuration, or create a simple servlet for the serving job, or as you're using PrimeFaces, just use <p:fileDownload> or <p:graphicImage> with StreamedContent pointing to the desired FileInputStream.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thx you very much for your answer! Just one question: What way do you recommend to make the upload folder as a system property? – maximus Apr 23 '13 at 08:42
  • 1
    Add it as VM argument to server startup script. E.g. `-Dupload.location="/resources/uploads"`. See also among others this answer http://stackoverflow.com/questions/14915887/path-inside-an-web-app-not-a-direct-link/14917577#14917577 – BalusC Apr 23 '13 at 10:20