<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>通过链接下载文件</h1> <a href="/day06/download/cors.zip">压缩包</a> <a href="/day06/download/1.png">图片</a> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>通过链接下载文件</h1> <a href="/day06/download/cors.zip">压缩包</a> <a href="/day06/download/1.png">图片</a> <h1>通过servlet程序下载文件</h1> <a href="/day06/ServletDownload?filename=cors.zip">压缩包</a> <a href="/day06/ServletDownload?filename=1.png">图片</a> </body> </html>
package com.lsgjzhuwei.servlet.response;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDownload
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class ServletDownload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletDownload() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获得请求文件名
String filename = request.getParameter("filename");
System.out.println(filename);
//设置文件MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + filename);
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
String filePath = this.getClass().getClassLoader().getResource("").toURI().getPath()
+ "/exportPdf.pdf"; //文件在项目中的路径
File outfile = new File(filePath);
String filename = outfile.getName();// 获取文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(
filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer); //读取文件流
fis.close();
response.reset(); //重置结果集
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.replaceAll(" ", "").getBytes("utf-8"),
"iso8859-1")); //返回头 文件名
response.addHeader("Content-Length", "" + outfile.length()); //返回头 文件大小
response.setContentType("application/octet-stream"); //设置数据种类
//获取返回体输出权
OutputStream os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer); // 输出文件
os.flush();
os.close();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有