这是一个Servlet,把一个图片读进来。  
public  class  RequestHeaderExample  extends  HttpServlet  {  
 
       ResourceBundle  rb  =  ResourceBundle.getBundle("LocalStrings");  
 
       public  void  doGet(HttpServletRequest  request,  
                                           HttpServletResponse  response)  
               throws  IOException,  ServletException  
       {  
               response.setContentType("application/x-www-form-urlencoded");  
               response.setHeader("ReturnCode",  "0");  
               DataOutputStream  dos  =  new  DataOutputStream(response.getOutputStream());  
               File  fi  =  new  File("D:\\2005-05\\aa.PSD");  
               FileInputStream  fos  =  new  FileInputStream(fi);  
               byte[]  rr  =  new  byte[  (int)  fi.length()];  
               fos.read(rr,  0,  rr.length);  
               dos.write(rr,0,rr.length);  
               dos.flush();  
               dos.close();  
       }  
 
       public  void  doPost(HttpServletRequest  request,  
                                           HttpServletResponse  response)  
               throws  IOException,  ServletException  
       {  
               doGet(request,  response);  
       }  
}  
 
在客户端写一个java application程序,得到这副图像,下面是我写的  
 
public  class  Receive  {  
 
           public  static  void  main(String[]  args)  {  
                       try  {  
                                   URL  url  =  new  URL("http://210.42.25.205:8080/servlets-examples/servlet/RequestHeaderExample");  
                                   HttpURLConnection  huc  =  (HttpURLConnection)url.openConnection();  
                                   huc.setRequestMethod("POST");  
                                   huc.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");  
                                   huc.getOutputStream();  //这个地方如何写?????把得到的信息保存到文件中,成为一个图像文件???  
                                     
 
                       }  
                       catch(Exception  ex)  {  
                                   System.out.println(ex.getMessage());  
                       }  
           }  
}  

解决方案 »

  1.   

    直接将字节码或者response出即可显示出来
      

  2.   

    huc.getOutputStream();
    既然是取得流应该是huc.getInputStream();吧
    InputStream in=huc.getInputStream();
    OutputStream out=new OutputStream("保存文件路径",true);
    byte[] bt=new byte[  (int) in.length()]; 
    in.read(bt);
    out.write(bt);
    in.close();
    out.close();
    这是我的思路,不知道对不对
      

  3.   

    InputStream没有length();方法,想办法把数据放到byte[]中,我觉得大概思路应该是对的
    如果错了请告知
      

  4.   

    InputStream没有length();方法,想办法把数据放到byte[]中,我觉得大概思路应该是对的
    如果错了请告知
      

  5.   

    InputStream  根本没有length这个属性也没有length()这个方法,