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.
You are using
CommonsIO
? ThatFileUtils
it’s hers?– Renan Gomes
Yes belongs to the Fileutils belongs to the Commonsio apache @rrnan
– João Manolo
If I’m not mistaken, she has a method that serves precisely what she needs.
FileUtils.copyFile(source, destino);
, both parameters areFile
.– Renan Gomes
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.
– João Manolo
Did your
folder.mkdirs();
is not failing? This method isboolean
, and you should check if it returnstrue
.– Victor Stafusa
Ah, and by the way, you can put the
ImageIO.write(blablabla);
after the blockif
to avoid having to repeat it in theelse
. This makes it easier to understand. Leave inside the blockif
only themkdirs
.– Victor Stafusa
Well noted @Victor :)
– João Manolo