public static void downLoadFile(String basePath, String fileName, HttpServletResponse resp) {
//设置响应头
resp.setContentType("application/x-msdownload");
resp.setHeader("content-disposition","attachment;filename="+URLUtil.encodeURL(fileName));
FileInputStream fIn = null;
OutputStream out = null;
try {
File file = new File(basePath + fileName);
//读取下载文件
fIn = new FileInputStream(file);
//输出流
out = resp.getOutputStream();
//缓冲区
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fIn.read(buffer,0,buffer.length))!=-1){
//将缓冲区的数据输出到浏览器,实现文件下载
out.write(buffer,0,len);
}
out.flush();
}catch (FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch (IOException ioe) {
ioe.printStackTrace();
}finally {
close(fIn,out);
}
}
commons-fileupload下载文件
最新推荐文章于 2017-05-05 22:16:13 发布