Skip to content

Instantly share code, notes, and snippets.

@dilnei
Created August 6, 2014 17:12
Show Gist options
  • Save dilnei/13ac1665b0409c969224 to your computer and use it in GitHub Desktop.
Save dilnei/13ac1665b0409c969224 to your computer and use it in GitHub Desktop.
>Classe que contém métodos de I/O.
package br.com.risingforce.funcoes;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
* <b>Classe que contém métodos de I/O.</b>
*
* @author Dlnei Cunha.
*/
public class WriteFileInFileSystem {
/**
* <b>Método responsável por ler e escrever um stream de dados.</b>
*
* @param stream
* @param fos
* @throws IOException
*/
public void writeFile(InputStream stream, FileOutputStream fos) throws IOException {
byte[] buffer = new byte[1024];
while (stream.read(buffer) > 0) {
fos.write(buffer);
}
fos.close();
stream.close();
}
/**
* <b>Método responsável por salvar a imagem em FileSystem.</b>
*
* @param bufferedImage
* @param nameImage
* @param pathImage
* @param extension
* @throws Exception - quando algum parâmetro for nulo.
*/
public void saveImage(BufferedImage bufferedImage, String nameImage, String pathImage, String extension) throws Exception {
if (bufferedImage == null) {
throw new Exception("Problemas ao ler o buffer da imagem, verifique se esta enviando o buffer corretamente.");
}
if (nameImage == null || nameImage.isEmpty()) {
throw new Exception("O nome da imagem não foi localizado, você precisa informar o nome da imagem corretamente.");
}
if (pathImage == null || pathImage.isEmpty()) {
throw new Exception("O caminho da imagem não foi localizado, você precisa informar o caminho da imagem corretamente.");
}
if (extension == null || extension.isEmpty()) {
throw new Exception("Extensão da imagem não localizado, você precisa informar o caminho da imagem corretamente.");
}
File filePathImage = new File(pathImage);
try {
ImageIO.write(bufferedImage, extension, new File(filePathImage.getAbsolutePath() + File.separator + nameImage + "." + extension));
} catch (IOException ex) {
Logger.getLogger(WriteFileInFileSystem.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment