Question

I have function like this in my Bean:

public String uploadFile(UploadedFile uploadedFile) {
    logger.info("Enter: uploadFile(UploadedFile uploadedFile).");
    String name = uploadedFile.getName();
    String extension = name.substring(name.length() - 3);
    if (extension.contentEquals("peg")) {
        extension = "jpeg";
    }

    RandomString rs = new RandomString(RANDOM_PHOTO_NAME_LENGTH);
    this.randomPhotoName = rs.nextString();

    String fileName = this.randomPhotoName + "." + extension;
    logger.info("File name: " + name + ". Extension: " + extension + ". New fileName: " + fileName);
    ServletContext sc = (ServletContext) FacesContext.getCurrentInstance()
            .getExternalContext().getContext();

    File f = new File(
            sc.getRealPath(Constant.USER_FILE_PATH));
    if (!f.exists()) {
        logger.info("Folder "
                + Constant.USER_FILE_PATH
                + " nie istniej. Tworze nowy.");
        f.mkdirs();
    }
    File backupFile = new File(
            sc.getRealPath(Constant.USER_FILE_PATH
                    + fileName));

    InputStream in = null;
    OutputStream out = null;
    try {
        in = new BufferedInputStream(uploadedFile.getInputStream());
        out = new FileOutputStream(backupFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0)
            out.write(buf, 0, len);
    } catch (IOException ioe) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage msg = null;
        msg = new FacesMessage(
                "Pojawił się nieoczekiwany błąd przy uploadowaniu pliku.");

        context.addMessage(null, msg);
        logger.error(
                "Pojawił się nieoczekiwany błąd przy uploadowaniu pliku.",
                ioe);
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    logger.info("Exit: uploadFile(UploadedFile uploadedFile).");
    return fileName;
}

And all files are saved in tmp directory eg:

\jboss-5.1.0.GA\server\default\tmp\a006-czo4uq-gzzu4l42-1-gzzuk7xr-a2\TupTus.war\media\img\user-gallery\6u2fpgu3tkzniwg.JPG
Was it helpful?

Solution

Because all your files are built as:

File backupFile = new File(
            sc.getRealPath(Constant.USER_FILE_PATH
                    + fileName));

Sounds like sc.getRealPath() returns working directory that JBoss allocates for your application.

So, the real question is to you: where do you want to write the files? If not there, so where? If you prefer user temporary directory use new File(System.getProperty("java.io.tmpdir"), fileName) and write there.

If you want to be able to configure the path out of the box you can store this path either in DB or configuration file or pass it via your custom system properties when you are running JBoss using command line switch -D.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top