protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// String realPath = this.getServletContext().getRealPath("22 .png");
//获取需要下载文件的路径
String realPath = "F:/IdeaData/web2/servlet01/target/servlet01-1.0-SNAPSHOT/WEB-INF/classes/22.jpg";
System.out.println("文件下载路径" + realPath);
//下载文件名称获取
String filename = realPath.substring(realPath.lastIndexOf("\\" + 1));
//设置让浏览器支持下载我们需要的东西
resp.setHeader("Content-disposition","attachment;filename"+filename);
//获取文件输入流
FileInputStream fileInputStream = new FileInputStream(realPath);
//创建缓冲区
int i = 0;
byte[] buffer = new byte[1024];
//获取output stream对象
ServletOutputStream outputStream = resp.getOutputStream();
//将file output stream流写入buffer缓冲区,使用outputsstream将缓冲区文件输出到就客户端
while (fileInputStream.read(buffer)!=-1){
outputStream.write(buffer);
}
//关闭流资源
fileInputStream.close();
outputStream.close();
}