package com.ciweb.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.ciweb.common.base.CIWEBException;
/**
*
* 下载相关的模块工具类
*
* @author
* @version [版本号, 2011-9-26]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class DownloadUitl
{
private static int buffersize = 1024;
/**
* 下载指定文件
* @param path 路径
* @param fileName fileName
* @throws CIWEBException 可能出现的异常
* @see [类、类#方法、类#成员]
*/
public static void downloadData(String path, String fileName)
throws CIWEBException
{
HttpServletResponse response = ServletActionContext.getResponse();
BufferedInputStream in = null;
BufferedOutputStream out = null;
try
{
File downloadFile = new File(path);
if (downloadFile.isFile())
{
in = new BufferedInputStream(new FileInputStream(downloadFile));
out = new BufferedOutputStream(response.getOutputStream());
//这个就是弹出下载对话框的关键代码
response.setHeader("Content-disposition",
"attachment;filename="
+ URLEncoder.encode(fileName, "utf-8"));
byte[] buffer = new byte[buffersize];//创建字节大小
//循环含义:当输入流读取字节大于0时,就一直输出字节-------也就是下载
while (in.read(buffer) > 0)
{
out.write(buffer);
}
response.flushBuffer();
}
else
{
response.sendError(404, "您要找的资源文件或已过期");
}
}
catch (Exception e)
{
throw new CIWEBException("下载出错!");
}
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException e)
{
throw new CIWEBException("下载出错!");
}
}
}
/**
* 下载指定文件
* @param path 路径
* @throws CIWEBException 可能出现的异常
* @see [类、类#方法、类#成员]
*/
public static void downloadData(String path)
throws CIWEBException
{
HttpServletResponse response = ServletActionContext.getResponse();
BufferedInputStream in = null;
BufferedOutputStream out = null;
try
{
File downloadFile = new File(path);
if (downloadFile.isFile())
{
in = new BufferedInputStream(new FileInputStream(downloadFile));
out = new BufferedOutputStream(response.getOutputStream());
//这个就是弹出下载对话框的关键代码
response.setHeader("Content-disposition",
"attachment;filename=" + URLEncoder.encode(path, "utf-8"));
byte[] buffer = new byte[buffersize];//创建字节大小
//循环含义:当输入流读取字节大于0时,就一直输出字节-------也就是下载
while (in.read(buffer) > 0)
{
out.write(buffer);
}
response.flushBuffer();
}
else
{
response.sendError(404, "您要找的资源文件或已过期");
}
}
catch (Exception e)
{
throw new CIWEBException("下载出错!");
}
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException e)
{
throw new CIWEBException("下载出错!");
}
}
}
/**
* 导出为文件
* @param fileSource 字节流
* @param fileNameWithExt 文件名
* @param response response 对象
* @return 出错信息
* @throws Exception 异常信息
* @see [类、类#方法、类#成员]
*/
public static String exportToFile(byte[] fileSource,
String fileNameWithExt, HttpServletResponse response)
throws Exception
{
String errorMsg = "";
if (fileNameWithExt != null && (!"".equals(fileNameWithExt)))
{
try
{
setCustomHeader(fileNameWithExt, response);
InputStream is = new ByteArrayInputStream(fileSource);
copyDataToOsFromIsAndClearIs(is,
response.getOutputStream(),
true);
}
catch (IOException e)
{
errorMsg = e.getMessage();
throw e;
}
}
return errorMsg;
}
/**
* 保存为文件
* @param fileSource ByteArrayOutputStream 输入信息
* @param fileNameWithExt 带扩展名的文件名
* @param response 响应
* @throws Exception 异常信息
* @return 错误信息,如果为""表示没有错误
*/
public static String exportToFile(ByteArrayOutputStream fileSource,
String fileNameWithExt, HttpServletResponse response)
throws Exception
{
String errorMsg = "";
if (fileNameWithExt != null && (!"".equals(fileNameWithExt)))
{
try
{
setCustomHeader(fileNameWithExt, response);
copyDataToOsFromIsAndClearIs(toInputStream(fileSource),
response.getOutputStream(),
true);
}
catch (IOException e)
{
errorMsg = e.getMessage();
throw e;
}
}
return errorMsg;
}
/**
* <导出文件>
* <功能详细描述>
* @param path 文件路径
* @param response response
* @return 错误信息
* @throws CIWEBException 异常
* @see [类、类#方法、类#成员]
*/
public static String exportToFile(String path, HttpServletResponse response)
throws CIWEBException
{
String errorMsg = "";
if (path != null && (!"".equals(path)))
{
File file = new File(path);
if (file != null)
{
try
{
String fileNameWithExt = file.getName();//.substring(file.getName().lastIndexOf(".") + 1);
setCustomHeader(fileNameWithExt, response);
copyDataToOsFromIsAndClearIs(new FileInputStream(file),
response.getOutputStream(),
true);
}
catch (IOException e)
{
errorMsg = e.getMessage();
throw new CIWEBException(e);
}
catch (Exception e)
{
errorMsg = e.getMessage();
throw new CIWEBException(e);
}
}
}
return errorMsg;
}
/**
* <导出文件>
* <功能详细描述>
* @param fileSource 源文件流
* @param fileNameWithExt 文件名
* @param response response对象
* @return 出错信息
* @throws CIWEBException 异常信息
* @see [类、类#方法、类#成员]
*/
public static synchronized String exportToFile(InputStream fileSource,
String fileNameWithExt, HttpServletResponse response)
throws CIWEBException
{
String errorMsg = "";
try
{
setCustomHeader(fileNameWithExt, response);
copyDataToOsFromIsAndClearIs(fileSource,
response.getOutputStream(),
true);
}
catch (IOException e)
{
errorMsg = e.getMessage();
throw new CIWEBException(e);
}
catch (Exception e)
{
errorMsg = e.getMessage();
throw new CIWEBException(e);
}
return errorMsg;
}
/**
* 将数据从输入流里拷贝到输出流里,拷贝完后可释放输入流决定于isFree的值
* @param is 输入流
* @param os 输出流
* @param isFree 是否释放输入流
* @throws IOException IO异常
* @see [类、类#方法、类#成员]
*/
public static synchronized void copyDataToOsFromIsAndClearIs(
InputStream is, OutputStream os, boolean isFree)
throws IOException
{
if (is != null && os != null)
{
try
{
byte[] buffer = new byte[1024];
int tmpLen = is.read(buffer);
while (tmpLen > 0)
{
os.write(buffer);
tmpLen = is.read(buffer);
}
}
catch (IOException e)
{
throw e;
}
finally
{
os.flush();
os.close();
is.close();
}
}
}
/**
* 设置下载文件时的 response 的 Header
* <功能详细描述>
* @param fileNameWithExt 带扩展名的文件名
* @param response Http 响应
* @throws Exception Exception
* @see [类、类#方法、类#成员]
*/
public static void setCustomHeader(String fileNameWithExt,
HttpServletResponse response)
throws Exception
{
try
{
StringBuffer sb = new StringBuffer(50);
sb.append("attachment; filename=");
sb.append(fileNameWithExt);
response.setContentType(mapContentType(fileNameWithExt));
response.setHeader("Content-Disposition", new String(sb.toString()
.getBytes(), "ISO8859-1"));
}
catch (Exception e)
{
throw e;
}
}
/**
* 映射 设置下载文件头的消息
* @param fileName 文件名称
* @return 映射信息
*/
public static String mapContentType(String fileName)
{
String fileNameTmp = "";
if (fileName != null)
{
fileNameTmp = fileName.toLowerCase();
}
String ret = "application/x-msdownload;charset=GB2312";
if (fileNameTmp.endsWith("txt"))
{
ret = "text/plain;charset=GB2312";
}
else if (fileNameTmp.endsWith("gif"))
{
ret = "image/gif;charset=GB2312";
}
else if (fileNameTmp.endsWith("jpg"))
{
ret = "image/jpeg;charset=GB2312";
}
else if (fileNameTmp.endsWith("jpeg"))
{
ret = "image/jpeg;charset=GB2312";
}
else if (fileNameTmp.endsWith("jpe"))
{
ret = "image/jpeg;charset=GB2312";
}
else if (fileNameTmp.endsWith("zip"))
{
ret = "application/zip;charset=GB2312";
}
if (fileNameTmp.endsWith("rar"))
{
ret = "application/rar;charset=GB2312";
}
else if (fileNameTmp.endsWith("doc"))
{
ret = "application/msword";
}
else if (fileNameTmp.endsWith("ppt"))
{
ret = "application/vnd.ms-powerpoint;charset=GB2312";
}
else if (fileNameTmp.endsWith("xls"))
{
ret = "application/vnd.ms-excel;charset=GB2312";
}
else if (fileNameTmp.endsWith("html"))
{
ret = "text/html;charset=GB2312";
}
else if (fileNameTmp.endsWith("htm"))
{
ret = "text/html;charset=GB2312";
}
else if (fileNameTmp.endsWith("tif"))
{
ret = "image/tiff;charset=GB2312";
}
else if (fileNameTmp.endsWith("tiff"))
{
ret = "image/tiff;charset=GB2312";
}
else if (fileNameTmp.endsWith("pdf"))
{
ret = "application/pdf;charset=GB2312";
}
else if (fileNameTmp.endsWith("xml"))
{
ret = "application/xml;charset=GB2312";
}
return ret;
}
/**
* 从 InputStream 类型数据里 获取 byte[]类型数据
* @param is InputStream 类型数
* @return 从 InputStream 类型数据里 获取的 byte[]类型数据
* @throws IOException IOException
*/
public static byte[] toByteArrayFromInputStream(InputStream is)
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (is != null)
{
byte buffer[] = new byte[buffersize];
int byteRead;
while ((byteRead = is.read(buffer)) > 0)
{
baos.write(buffer, 0, byteRead);
}
}
return baos.toByteArray();
}
/**
* 将 ByteArrayOutputStream 转换成 InputStream
* @param baos ByteArrayOutputStream对象数据
* @return 转换成 的 InputStream
*/
protected static InputStream toInputStream(ByteArrayOutputStream baos)
{
return new ByteArrayInputStream(baos.toByteArray());
}
}