ExtJS 下载文件

40 篇文章 0 订阅

ExtJs前台:

 window.open('/fileUpload/downloadAddNum?id=' + id+"&fileName="+fileName+"&downloadName="+downloadName);

//注:

fileName:是文件的原名称,如 dog.jpg

downloadName:是文件下载显示名称,如 狗.jpg。


后台处理:

调用 downloadFile 方法:

注:filePath为物理路径。如:C:\images\dog.jpg

public static void downloadFile(String filePath,String downloadName,HttpServletResponse response){
		
	BufferedInputStream bis = null;
	BufferedOutputStream bos =null;
	OutputStream os = null;
	InputStream is = null;
	
	try {
		File downloadFile = new File(filePath);//物理路径
		is = new FileInputStream(downloadFile);
		bis = new BufferedInputStream(is);
		os = response.getOutputStream();
		bos = new BufferedOutputStream(os);
		
		downloadName = java.net.URLEncoder.encode(downloadName,"UTF-8");
		//fileName = new String(fileName.getBytes("UTF-8"),"GBK");//处理中文文件名问题
		response.reset();
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/octet-stream");
		response.setHeader("Content-disposition","attachment;filename="+downloadName);
		
		byte[] buffer =new byte[8192];
		int len=0;
		
		while( (len = bis.read(buffer)) > 0){
			bos.write(buffer,0,len);
		}
		bos.flush();
		is.close();
		bis.close();
		os.close();
		bos.close();
		System.gc();
	} catch (Exception e) {
		e.printStackTrace();
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值