一、文件下载
二、java中压缩文件,并下载,实例链接:
[url]http://blog.163.com/chiyunjuan@126/blog/static/34119412201182055051171/[/url]
@RequestMapping(value="/download")
public void download(ModelAndView mav, HttpServletRequest request,
HttpServletResponse response, @RequestParam(value="id",required=true) String id) {
Datadownload data = null;
//根据id获得资料对象
data = datadownloadSelectService.getByID(Long.parseLong(id));
//客户端IP
String ip = request.getLocalAddr();
//得到文件真实路径
ServletContext context = request.getSession().getServletContext();
String filename = context.getRealPath(data.getFile_url());
//新的文件名+扩展名
String newname = data.getOldname()+"."+data.getExpand();
try {
// 此文件是否存在
String path = filename;
File file = new File(path);
if (file.exists()&&data.getData_status()!=5) {
//存在的情况下,设置response参数
response.setContentType("application/force-download");
String oldname = URLEncoder.encode(newname, "UTF-8");
oldname = newname;
if(request.getHeader("user-agent").indexOf("MSIE") != -1) {
oldname = java.net.URLEncoder.encode(oldname,"utf-8");
} else {
oldname = new String(oldname.getBytes("utf-8"),"iso-8859-1") ;
}
response.setHeader("Content-Disposition",
"attachment;filename=" + oldname);
response.setContentLength((int) file.length());
//参数设置结束
//文件进入输入流
InputStream in = new FileInputStream(file);
//设置输出流 为response
OutputStream out = response.getOutputStream();
//设置缓冲区并读出
byte[] b = new byte[1024];
int len = -1;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
} else {
System.err.println("你下载文件已经不存在...");
}
} catch (Exception e) {
}
}
二、java中压缩文件,并下载,实例链接:
[url]http://blog.163.com/chiyunjuan@126/blog/static/34119412201182055051171/[/url]