Copy Outputstream object to a file

Asked

Viewed 377 times

3

I confess it’s the first time I’ve worked with Stream’s and File in Java.

Currently I use the Spring (Core) in my application, I soon use a simple copy utility feature stream’s and file’s called FileCopyUtils (I don’t know if he meets my requirement!).

Well, the point is that I have a method that aims to get an image that is found in a temporary directory (Ex:. /tem/joocebox-img/Destination/MINHA_ IMG.jpg) and save into a folder on the machine (Ex:. /app/joocebox-img/.../MINHA_IMG.jpg). Below follows the method to illustrate better with 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 necessary I can post the methods of resize image, in order to perhaps improve something.

EDITION

I tried to use the ImageIO.write();, but unsuccessfully 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 bytes to the created file.

  • 1

    You are using CommonsIO? That FileUtils it’s hers?

  • Yes belongs to the Fileutils belongs to the Commonsio apache @rrnan

  • 1

    If I’m not mistaken, she has a method that serves precisely what she needs. FileUtils.copyFile(source, destino);, both parameters are File.

  • So there is a class of Commonsio calling for Imagery. I edited the question using it, but I still can’t get the expected result.

  • 1

    Did your folder.mkdirs(); is not failing? This method is boolean, and you should check if it returns true.

  • 1

    Ah, and by the way, you can put the ImageIO.write(blablabla); after the block if to avoid having to repeat it in the else. This makes it easier to understand. Leave inside the block if only the mkdirs.

  • Well noted @Victor :)

Show 2 more comments

1 answer

1

Well, I managed to "come up with" a solution by altering the return of my method of resize imaging (resizeDestinationImageToThumb()) for a array of byte’s. Hence the utility class of Spring FileCopyUtils.copy(byte[] in, File out); helped me in the copy of the file. Below how it was fixed:

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));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.