1.获得文件路径,得到文件名
2.告诉浏览器要下载文件和下载文件的格式
3.response获得输出流,输出文件
response.getWriter()示例
package com.learning.servlet; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; 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 DownLoadServlet */ @WebServlet("/servlet/downLoadServlet") public class DownLoadServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //文件下载 //要下载的文件名 String filename="123.txt"; //设置文件名的编码 if(request.getHeader("user-agent").toLowerCase().contains("msie")){ filename = URLEncoder.encode(filename, "UTF-8");//将不安全的文件名改为UTF-8格式 }else{ filename = new String(filename.getBytes("UTF-8"),"iso-8859-1");//火狐浏览器 } //告诉浏览器要下载文件 response.setHeader("content-disposition", "attachment;filename="+filename); //告诉浏览器要下载的文件格式 response.setContentType(this.getServletContext().getMimeType(filename)); //获得输出流 PrintWriter out = response.getWriter(); out.write("tom,123 \n"); out.write("jerry,123 \n"); out.write("john,123 \n"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
下载图片示例:
/** * */ package com.learning.servlet; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; 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; /** * @author 杰 * */ public class ServletDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //先获得文件的路径 String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/美女.jpg"); //通过文件流来读取文件 FileInputStream fileInputStream=new FileInputStream(new File(realPath)); //获取文件名 String filename=realPath.substring(realPath.lastIndexOf("/")+1); //设置文件名的编码 if(request.getHeader("user-agent").toLowerCase().contains("msie")){ filename = URLEncoder.encode(filename, "UTF-8");//将不安全的文件名改为UTF-8格式 }else{ filename = new String(filename.getBytes("UTF-8"),"iso-8859-1");//火狐浏览器 } //告诉浏览器要下载文件 response.setHeader("content-disposition","attachment;filename="+filename); //告诉浏览器文件的格式 response.setContentType(this.getServletContext().getMimeType(filename)); //输出文件 ServletOutputStream outputStream = response.getOutputStream(); int len=1; byte[] b=new byte[1024]; while ((len=fileInputStream.read(b))!=-1) { outputStream.write(b, 0, len); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
时间: 2024-11-05 23:04:48