Servlet文件下载

servlet文件下载

/**
 * @author Administrator
 * 文件下载
 */
public class DownloadServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		//获取uuidRealFileName
		String uuidRealFileName = request.getParameter("uuidRealFileName");
		//还原
		byte[] buf = uuidRealFileName.getBytes("ISO8859-1");
		//手工解码
		uuidRealFileName = new String(buf,"UTF-8");
		//获取realFileName
		int index = uuidRealFileName.indexOf("_");
		String realFileName = uuidRealFileName.substring(index+1);
		//通知浏览器以下载方式打开文件
		//response.setHeader("content-type","application/x-msdownload");
		response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(realFileName,"UTF-8"));
		//以IO流方式将服务端的文件复制给客户端
		String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
		String subUploadPath = makeSubUpload(uploadPath,uuidRealFileName);
		InputStream is = new FileInputStream(subUploadPath + "/" + uuidRealFileName);
		OutputStream os = response.getOutputStream();
		buf = new byte[2048];
		int len = 0;
		while((len=is.read(buf))>0){
			os.write(buf,0,len);
		}
		os.close();
		is.close();
	}
	//通过这个算法,得到下载文件的位置
	private String makeSubUpload(String uploadPath,String uuidRealFileName){
		//获取hashCode整型值
		int code = uuidRealFileName.hashCode();
		//第一级子目录
		int dir1 = code & 0xF;//12
		//第二级子目录
		int dir2 = ( code >> 1 ) & 0xF;//6 
		//创建这些子目录
		File file = new File(uploadPath+"/"+dir1+"/"+dir2);
		//如果不存在该子目录
		if(!file.exists()){
			//连续创建2个子目录
			file.mkdirs();
		}
		//将创建后的子目录返回
		return file.getPath();
	} 
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值