您的位置:首页 > Web前端 > JavaScript

ExtJS 下载文件

2016-01-25 11:51 429 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: