/**
* 附件下载方式放回
**/
public static void downloadFile(String fileName, InputStream inputStream, HttpServletResponse response) throws IOException, BusinessServiceException
{
if(inputStream != null)
{
ServletOutputStream os = response.getOutputStream();
try(BufferedInputStream bis = new BufferedInputStream(inputStream); BufferedOutputStream bos = new BufferedOutputStream(os))
{
response.setCharacterEncoding("UTF-8");
response.setContentType("application/force-download;");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
int length;
byte[] bytes = new byte[2048];
while(-1 != (length = bis.read(bytes)))
{
bos.write(bytes, 0, length);
}
}
catch(IOException e)
{
log.error("download exception:", e);
throw new BusinessServiceException("下载文件失败!");
}
}
}
/**
* 浏览器中打开预览方式
**/
public static void previewFile(String fileName, InputStream inputStream, HttpServletResponse response) throws IOException, BusinessServiceException
{
if(inputStream != null)
{
ServletOutputStream os = response.getOutputStream();
try(BufferedInputStream bis = new BufferedInputStream(inputStream); BufferedOutputStream bos = new BufferedOutputStream(os))
{
response.setCharacterEncoding("UTF-8");
response.setContentType("image/png");
response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
int length;
byte[] bytes = new byte[2048];
while(-1 != (length = bis.read(bytes)))
{
bos.write(bytes, 0, length);
}
}
catch(IOException e)
{
log.error("previewFile exception:", e);
throw new BusinessServiceException("文件预览失败!");
}
}
}