gistfile1.txt package com.zr;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax
package com.zr; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class DownPicture */ @WebServlet("/DownPicture") @MultipartConfig public class DownPicture extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DownPicture() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //处理文件下载 //通过设置响应头 response.setContentType("application/x-download"); response.addHeader("Content-Dsiposition", "attachment;filename=pic.jpg"); //将真实的图片送到浏览器 OutputStream out = response.getOutputStream(); //获取地址 String downaddress = request.getParameter("downAddress"); //3,创建文件输入流对象FileInputStream InputStream in = new FileInputStream(downaddress); //字节数组 //4,定义字节数组 byte[] buffer = new byte[1024]; //len是实际的长度 int len = in.read(buffer); while(len != -1){ //5,将图片写入输出流中(相当于文件拷贝) out.write(buffer, 0, len);//长度为len ,从0开始 len = in.read(buffer); } //6,关闭 in.close(); out.close(); } }