HttpServletResponse 下载功能的实现
DiskFileItemFactory dfif = new DiskFileItemFactory();//创建硬盘文件项工厂
ServletFileUpload sfu = new ServletFileUpload(dfif);//创建文件组选项
sfu.setHeaderEncoding("utf-8");
sfu.setFileSizeMax(30*1024*1024);//设置上传文件大小
List items = null;
try{
items = sfu.parseRequest(req);
}catch(FileUploadException e){
e.printStackTrace();
}
Iterator it = items.iterator();
int i =1;
while(it.hasNext()){
FileItem fileitem = (FileItem)it.next();
ServletOutputStream outputStream = resp.getOutputStream();
resp.setHeader("content-disposition","attachment;filename="+name);
while((ss = value.read())!=-1){
outputStream.write(ss);
}
outputStream.flush();
outputStream.close();
后记:
如果name的命名是中文,需要转码:name = java.net.URLENconder.encode(name,"utf-8")
方法二
代理方式:resp.setContentType("application/x-msdownload");