package cn.edu.Response; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; 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; import org.omg.CORBA_2_3.portable.OutputStream; public class ResponseDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path=this.getServletContext().getRealPath("/download/阿木木.jpg"); String filename=path.substring(path.lastIndexOf("\\")+1); //如果下载名是中文,则文件名需要经过url编码URLEncoder.encode(filename,"UTF-8")(平常直接+文件名即可) response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8")); InputStream in=null; ServletOutputStream out=null; try{ in=new FileInputStream(path); int len=0; byte buffer[] = new byte[1024]; out=response.getOutputStream(); while((len=in.read(buffer))>0) { out.write(buffer,0,len); } }finally{ if(in!=null){ try{ in.close(); }catch(Exception e){ e.printStackTrace(); } } if(in!=null){ try{ out.close(); }catch(Exception e){ e.printStackTrace(); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }