标签:
获取要下载的文件,主要是在服务器上,这种方式会有一个插件,感觉不错!!
直接在servlet或controller上复制这段代码就能轻松实现下载功能了
String photo = list.get(0).get("photo");
photo = new String(photo.getBytes("iso8859-1"),"UTF-8");
//文件在服务器上的路径
File file = new File("E://shxt//soft//apache-tomcat-7.0.64//webapps//teacher//image//" + photo);
String realname = photo.substring(photo.indexOf("_")+1);
response.setHeader("content-disposition", "attachment;photo=" + URLEncoder.encode(realname, "UTF-8"));
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte buffer[] = new byte[2048];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
in.close();
out.close();
标签:
原文地址:http://www.cnblogs.com/yuxiliang/p/5799453.html