1

Hi to you all java experts.

I have this piece of code I could finally put together that works: (it's mostly java with a little ADF code)

public String upload(){
    UploadedFile myfile = this.getFile();

    FacesContext fctx = FacesContext.getCurrentInstance();
    ServletContext servletCtx =
        (ServletContext)fctx.getExternalContext().getContext();
    String imageDirPath = servletCtx.getRealPath("/");
    String nomdefichier = myfile.getFilename();
    String mimetype = nomdefichier.substring(nomdefichier.length() - 3);
    try {
        InputStream inputStream = myfile.getInputStream();
        BufferedImage input = ImageIO.read(inputStream);

        File outputFile =
            new File( System.getProperty("user.home") + File.separator + this.path + File.separator + nomdefichier);         
        ImageIO.write(input, mimetype, outputFile);

    } catch (Exception ex) {
        // handle exception
    }
    FacesMessage message =
        new FacesMessage(mimetype + "Successfully uploaded file " + nomdefichier +
                         " (" + myfile.getLength() + " bytes)" + mimetype);
    fctx.addMessage(null, message);

    return null;

}

This codes uploads a picture just fine. I would really like to know if there is a file equivalent to ImageIO.write so that I could upload PDF, DOCX and such.

Thanks in advance for any response.

Best regards.

Marc Arbour

  • 4
    Files are just bytes. You can upload any kind of file with just regular streams. Your example code is unnecessarily creating a `BufferedImage`, when you could just write the bytes you're getting from the inputstream directly to disk. – Kayaman Jul 30 '14 at 15:18
  • Would you be kind enough to detail a little or to point me towards a tutorial so that I could learn not to use a bufferedimage but rather bytes. Thanks – Marc Arbour Jul 30 '14 at 15:21
  • 2
    An illustration of what @Kayaman is referring to can be found [here](http://stackoverflow.com/q/43157/1076463) – Robin Jul 30 '14 at 15:23

2 Answers2

2

A simplified version of your code could be written as follows (omitting some of the JSF related stuff).

InputStream in = myFile.getInputStream();
Files.copy(in, Paths.get(yourPath));
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Hi again Kayaman, your code works but it seems to remove a couple of bytes to each file uploaded making my PDF corrupted, Is there something I am missing? – Marc Arbour Jul 30 '14 at 15:45
  • There's not much that could be missing. `Files.copy()` handles things rather automatically. How many bytes are missing? – Kayaman Jul 30 '14 at 15:48
  • 8 to be precise, it's the same with a word document... 8 bytes missing... here is my code `Files.copy(inputStream, Paths.get(System.getProperty("user.home") + File.separator + this.path + File.separator + nomdefichier), REPLACE_EXISTING COPY_ATTRIBUTES)` – Marc Arbour Jul 30 '14 at 15:56
  • I guess I should post this into another question since I marked this one as resolved. Thanks guys. – Marc Arbour Jul 30 '14 at 17:13
0

You can write a byte array or an InputStream to a file with the java.nio.file.Files class (since Java 1.7).

// Saving data from an inputstream to a file:
Files.copy(inputStream, targetPath, copyOptions...);

// Saving a byte array to a file:
Files.write(path, byteArray, openOptions...);
icza
  • 389,944
  • 63
  • 907
  • 827