Copy OutputStream object to a file

3

I confess that this is the first time I work with Stream's and File in Java .

I currently use Spring in my application, so I use a simple stream utility utility and file's called FileCopyUtils (I do not know if it satisfies my requirement!).

Well, the question is do I have a method that aims to get an image that is in a temporary directory (eg: / has / joocebox-img / destination / IMG.jpg ) and writes to a folder on the machine (eg: / app / joocebox-img /.../ MINHA_IMG.jpg ). Here is the method to better illustrate the comments to facilitate understanding:

public void copyThumbnailFilesToServer(String subdomain, String oldPath, String destinationName, String fileName) throws FileNotFoundException, IOException {

    //Subdomain: O sub-domínio corrente na aplicação (estou usando multi tenancy)
    // oldPath: Caminho da minha imagem dentro do diretorio /tmp
    //destinationName e fileName: atributos usados para montar o caminho onde o arquivo deve ser salvo

    //folder: usado na condicional if para verificar se a pasta onde será persistida a imagem (caso o caminho exista)
    File folder = new File(new JooceBoxProperties().getPathThumbnailImage(subdomain) + destinationName);

    //chamada de um metodo que realiza o resize da minha imagem retornando um OutputStream
    OutputStream resizeDestinationImageToThumb = resizeDestinationImageToThumb(oldPath);

    if (!folder.exists()) {
        folder.mkdirs();

        //Aqui é onde estou perdido. Preciso encontrar um modo (não necessariamente com o FileCopyUtils) de gravar o resizeDestinationImageToThumb dentro de um arquivo. 
        FileCopyUtils.copy(FileUtils.openInputStream(new File(oldPath)), resizeDestinationImageToThumb);

    } else {

        FileCopyUtils.copy(FileUtils.openInputStream(new File(oldPath)), resizeDestinationImageToThumb);
    }
}

The basic difficulty is described above. If needed, I can post image resize methods in order to improve something.

EDITION

I tried to use the ImageIO.write(); approach, but without success as shown below:

public void copyThumbnailFilesToServer(String subdomain, String oldPath, String destinationName, String fileName) throws FileNotFoundException, IOException {

    File folder = new File(new JooceBoxProperties().getPathThumbnailImage(subdomain) + destinationName);
    FileUtils.touch(new File(new JooceBoxProperties().getPathThumbnailImage(subdomain) + destinationName + "/" + fileName));

    OutputStream resizeDestinationImageToThumb = resizeDestinationImageToThumb(oldPath);

    if (!folder.exists()) {
        folder.mkdirs();
        //FileCopyUtils.copy(FileUtils.openInputStream(new File(oldPath)), resizeDestinationImageToThumb);
        ImageIO.write((RenderedImage) resizeDestinationImageToThumb, "jpg", new File(new JooceBoxProperties().getPathThumbnailImage(subdomain) + destinationName + "/" + fileName));
    } else {
        ImageIO.write((RenderedImage) resizeDestinationImageToThumb, "jpg", new File(new JooceBoxProperties().getPathThumbnailImage(subdomain) + destinationName + "/" + fileName));
        //FileCopyUtils.copy(FileUtils.openInputStream(new File(oldPath)), resizeDestinationImageToThumb);
    }
}

It does not copy the bytes to the created file.

    
asked by anonymous 03.02.2015 / 17:51

1 answer

1

Well, I've been able to "figure out" a solution by changing the return of my resize method from images ( resizeDestinationImageToThumb() ) to array byte's in>. Soon the Spring FileCopyUtils.copy(byte[] in, File out); utility class helped me to copy the file. Here's how it was solved:

public void copyThumbnailFilesToServer(String subdomain, String oldPath, String destinationName, String fileName) throws FileNotFoundException, IOException{

    File folder = new File(new JooceBoxProperties().getPathThumbnailImage(subdomain)+destinationName);
    FileUtils.touch(new File(new JooceBoxProperties().getPathThumbnailImage(subdomain)+destinationName+"/"+fileName));

    if (!folder.exists())
        folder.mkdirs();

    FileCopyUtils.copy(resizeDestinationImageToThumb(oldPath), new File(new JooceBoxProperties().getPathThumbnailImage(subdomain)+destinationName+"/"+fileName));
}
    
04.02.2015 / 02:41