<a>标签下载
1.新建Dynamic工程并命名为webdemo
2.在WebContent下新建文件夹download并在该文件夹下放置一张图片lyf.jpg
3.在WebContent下新建jsp命名为index.jsp
4.运行tomcat,访问localhost:8080/webdemo跳转到首页index.jsp,点击下载
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td><a href="${ pageContext.request.contextPath }/download/lyf.jpg" download="刘亦菲.jpg">下载:</a></td>
</tr>
</table>
</body>
</html>
servlet下载
1.新建Dynamic工程并命名为webdemo
2.在WebContent下新建文件夹download并在该文件夹下放置一张图片刘亦菲.jpg
3.新建servlet命名为MyServlet,设置访问路径为myServlet
4.运行tomcat,访问localhost:8080/webdemo/myServlet下载图片
package pack;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.misc.BASE64Encoder;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取要下载的文件名
String fileName = "刘亦菲.jpg";
// 获取该文件在服务端的位置
String realPath = this.getServletContext().getRealPath("download/" + fileName);
// 获取文件的输入流
FileInputStream in = new FileInputStream(realPath);
// 获取输出流
ServletOutputStream out = response.getOutputStream();
// 获取访问的浏览器信息
String agent = request.getHeader("User-Agent");
// 判断浏览器类型
if (agent.contains("MSIE")) {// IE浏览器
fileName = URLEncoder.encode(fileName, "utf-8");
fileName = fileName.replace(",", " ");
} else if (agent.contains("Firefox")) {// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
fileName = "=?utf-8?B?" + base64Encoder.encode(fileName.getBytes("utf-8")) + "?=";
} else {// 其他浏览器,例如谷歌浏览器
fileName = URLEncoder.encode(fileName, "utf-8");
}
// 告诉浏览器文件的类型
response.setContentType(getServletContext().getMimeType(fileName));
// 告诉浏览器打开该文件的方式是下载
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
int len = 0;
byte[] bys = new byte[1024];
while ((len = in.read(bys)) > 0) {
out.write(bys, 0, len);
}
in.close();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
springmvc下载
搭建ssm框架,要完成下载功能还需满足下列条件
1.需要导入包javax.servlet-api(版本3.0.1及以上)
2.为防止和tomcat自带的servlet-api发生冲突,所以scope为provided
3.在调用的方法的形参中加入HttpServletRequest request
4.使用String realPath = req.getServletContext().getRealPath("/download");获取webapp(只能是webapp)下download文件夹的绝对路径
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
@RequestMapping("download")
public ResponseEntity<byte[]> download(HttpServletRequest req) throws IOException {
// 获取webapp下download文件夹的绝对路径
String realPath = req.getServletContext().getRealPath("/download");
// 拼接要下载资源的绝对路径
String path=realPath+"/刘亦菲.jpg";
File file=new File(path);
HttpHeaders headers = new HttpHeaders();
// 解决中文名称乱码问题
String fileName=new String("刘亦菲.jpg".getBytes("UTF-8"),"iso-8859-1");
// 告诉浏览器不要直接打开该文件,而是下载
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
byte[] bys = FileUtils.readFileToByteArray(file);
return new ResponseEntity<byte[]>(bys, headers, HttpStatus.CREATED);
}