package com.infrastructure.project.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.infrastructure.project.common.SysConstant;

public class DownLoadUtil {
	/**
	 * 
	 * @param response
	 * @param request
	 * @param filePath
	 * @param fileName
	 * @param type
	 * @throws IOException
	 */
 public static void downLoad(HttpServletResponse response,HttpServletRequest request,String filePath ,String fileName,String type) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		java.io.BufferedInputStream bis = null;
		java.io.BufferedOutputStream bos = null;
		try {
			File file = null;
			if(SysConstant.downLoadType.DOWNLOAD_PDF.equals(type) || type == SysConstant.downLoadType.DOWNLOAD_PDF){
				file = new File(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_PDF);
			}else if(SysConstant.downLoadType.DOWNLOAD_EXCEL.equals(type) || type == SysConstant.downLoadType.DOWNLOAD_EXCEL){
				file = new File(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_EXCEL);
			}else if(SysConstant.downLoadType.DOWNLOAD_WORD.equals(type) || type == SysConstant.downLoadType.DOWNLOAD_WORD){
				file = new File(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_WORD);
			}else if(SysConstant.downLoadType.DOWNLOAD_ZIP.equals(type) || type == SysConstant.downLoadType.DOWNLOAD_ZIP){
				file = new File(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_ZIP);
			}else if(SysConstant.downLoadType.DOWNLOAD_TXT.equals(type) || type == SysConstant.downLoadType.DOWNLOAD_TXT){
				file = new File(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_TXT);
			}else{
				file = new File(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_HTML);
			}
			long fileLength = file.length();
			response.setContentType("application/x-msdownload;");
			response.setHeader("Content-disposition", "attachment; filename="+ new String(file.getName().getBytes("utf-8"), "ISO8859-1"));
			response.setHeader("Content-Length", String.valueOf(fileLength));
			bis = new BufferedInputStream(new FileInputStream(file));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
}
}
最近下载更多
wbbhappy  LV13 1月13日
sswert  LV2 2022年3月18日
天险无涯  LV15 2022年1月19日
北凉 _徐凤年  LV4 2021年12月21日
aaaahao  LV13 2021年7月25日
alexgyp  LV6 2021年7月9日
soscat2000  LV3 2020年7月6日
libo1212  LV8 2020年5月9日
zmy001  LV11 2020年4月8日
490239876  LV7 2020年2月27日
最近浏览更多
wbbhappy  LV13 1月10日
2317696509  LV6 2023年12月15日
1529860026  LV24 2023年7月6日
yangxb2  LV10 2022年9月19日
Hachi6  LV13 2022年9月19日
jinnitom 2022年9月14日
暂无贡献等级
8战魂5无双8  LV43 2022年9月13日
是你爸爸啊100  LV5 2022年8月8日
暂无贡献等级
rookie_58  LV2 2022年6月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友