servlet提供下载图片服务

public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    	response.setContentType("text;html;charset=utf-8");
    	DataOutputStream output=new DataOutputStream(response.getOutputStream());
    	String imagePath = "D://home.jpg";
    	//http://localhost:8080/HelloWorldServlet/MyFirstServlet?username=xxxx&password=111
    	  String username=request.getParameter("username");
    	  String password=request.getParameter("password");
    	  System.out.println("username="+username);
    	  System.out.println("password="+password);
    	  System.out.println("servlet test success....");

    	  response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("home.jpg", "UTF-8"));   
    	  response.setHeader("Content-Type", "application/octet-stream"); 
    	  
    	  FileInputStream fis = new FileInputStream(imagePath);
          int size =fis.available(); //得到文件大小   
          byte data[]=new byte[size];   
          fis.read(data);  //读数据   
          fis.close(); 
    	  
          output.write(data);
          output.close();
    }


你可能感兴趣的:(servlet提供下载图片服务)