Skip to content

Instantly share code, notes, and snippets.

@ggdio
Created September 17, 2013 19:51
Show Gist options
  • Save ggdio/6599642 to your computer and use it in GitHub Desktop.
Save ggdio/6599642 to your computer and use it in GitHub Desktop.
Util download class for web file response
package br.com.ggdio.gists;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
/*
* Class for downloads
* @author Guilherme Dio
*/
public class Download
{
File arquivo;
MimeType tipoArquivo = null;
String nomeArquivo = null;
byte[] arrayBytes = null;
public Download(String caminho, String nomeArquivo)
{
this.arquivo = new File(caminho, nomeArquivo);
this.nomeArquivo = nomeArquivo;
}
public Download(String nomeArquivo)
{
this.arquivo = new File(nomeArquivo);
}
public Download(byte[] arrayBytes, String fileName)
{
setArrayBytes(arrayBytes);
this.nomeArquivo = fileName;
}
public MimeType getMimeType()
{
if (tipoArquivo == null)
{
if (arquivo != null)
{
if (arquivo.exists())
{
tipoArquivo = getFormatoArquivo(arquivo.toString());
}
}
else
{
tipoArquivo = getFormatoArquivo(nomeArquivo);
}
}
return tipoArquivo;
}
public String getNomeArquivo()
{
if (arquivo != null)
return arquivo.getName();
else if(nomeArquivo != null && !"".equals(nomeArquivo))
return nomeArquivo;
return null;
}
public static MimeType getFormatoArquivo(String nomeArquivo)
{
if (nomeArquivo != null)
{
int index = nomeArquivo.lastIndexOf(".");
if (index > -1 && (index < nomeArquivo.length() - 1))
{
String formatoExtensao = nomeArquivo.substring(index + 1).toUpperCase();
MimeType tipo = Enum.valueOf(MimeType.class,formatoExtensao);
return tipo;
}
}
return null;
}
public InputStream getInputStream() throws FileNotFoundException
{
if (arquivo != null && arquivo.exists() && !arquivo.isDirectory())
{
return new FileInputStream(arquivo);
}
else
{
if (arrayBytes == null)
arrayBytes = new byte[0];
return new ByteArrayInputStream(arrayBytes);
}
}
public void carregaDownload(HttpServletResponse response)
{
try
{
// Obtem o output stream
ServletOutputStream out = response.getOutputStream();
// Informa o tipo do arquivo
response.setContentType(getMimeType().formato());
if (getNomeArquivo() != null || arrayBytes != null)
{
if (nomeArquivo != "")
{
response.setHeader("Content-Disposition","attachment; filename=\"" + nomeArquivo + "\"");
}
else
{
response.setHeader("Content-Disposition","attachment; filename=\"" + getNomeArquivo() + "\"");
}
}
// Cria um buffer com 1 byte
byte[] buffer = new byte[1024];
// Obtem o output steam do arquivo
InputStream in = getInputStream();
int bytes = 0;
// Obtem todos os bytes do arquivo
while ((bytes = in.read(buffer)) != -1)
{
// Escreve os bytes no output
out.write(buffer, 0, bytes);
}
// Fecha o output stream do arquivo
in.close();
// Fecha o output stream do response
out.close();
}
catch (Exception e)
{
//log
}
}
public void setFormatoArquivo(MimeType tipoArquivo)
{
this.tipoArquivo = tipoArquivo;
}
public byte[] getArrayBytes()
{
return arrayBytes;
}
public void setArrayBytes(byte[] bs)
{
arrayBytes = bs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment