kangjianrong

java模拟post方式提交表单实现图片上传 - 动感超人的专栏 - 博客频道 - CSDN.NET



 

模拟表单html如下:

 


 
   
 
 
 
   
 
 
 
 
 
 

 

 

java代码如下:

 

  1. package   
  2.   
  3. import   
  4. import   
  5. import   
  6. import   
  7. import   
  8. import   
  9. import   
  10. import   
  11. import   
  12. import   
  13. import   
  14. import   
  15.   
  16. import   
  17.   
  18. import   
  19. import   
  20.   
  21. public class    
  22.   
  23.     /** 
  24.         
  25.        
  26.     public static void     
  27.           
  28.          "E:\\ziliao\\0.jpg";  
  29.           
  30.            "http://127.0.0.1:8080/minicms/up/up_result.jsp";  
  31.           
  32.             new    
  33.       
  34.         "name""testname");  
  35.   
  36.             new    
  37.           
  38.         "userfile",   
  39.           
  40.                
  41.           
  42.           
  43.           
  44.   
  45.       
  46.       
  47.       
  48.   
  49.     /** 
  50.        
  51.        
  52.         
  53.         
  54.         
  55.        
  56.        
  57.     public static        
  58.                  
  59.            "";  
  60.            null;  
  61.            "---------------------------123821742118716"//boundary就是request头和上传文件内容的分隔符  
  62.         try   
  63.                new   
  64.                  
  65.             5000);  
  66.             30000);  
  67.             true);  
  68.             true);  
  69.             false);  
  70.             "POST");  
  71.             "Connection""Keep-Alive");  
  72.               
  73.                     "User-Agent",  
  74.                             "Mozilla/5.0       );  
  75.             "Content-Type",  
  76.                     "multipart/form-data;     
  77.   
  78.                new   
  79.             //   
  80.             if   null)   
  81.                    new   
  82.                      
  83.                 while    
  84.                           
  85.                           
  86.                           
  87.                     if   null)   
  88.                         continue;  
  89.                       
  90.                     "\r\n").append("--").append(BOUNDARY).append(  
  91.                             "\r\n");  
  92.                     "Content-Disposition:    
  93.                                "\"\r\n\r\n");  
  94.                       
  95.                   
  96.                   
  97.               
  98.   
  99.             //   
  100.             if   null)   
  101.                      
  102.                 while    
  103.                           
  104.                           
  105.                           
  106.                     if   null)   
  107.                         continue;  
  108.                       
  109.                        new   
  110.                          
  111.                        new   
  112.                               
  113.                     if ".png"))   
  114.                           "image/png";  
  115.                       
  116.                     if   null  ""))   
  117.                           "application/octet-stream";  
  118.                       
  119.   
  120.                        new   
  121.                     "\r\n").append("--").append(BOUNDARY).append(  
  122.                             "\r\n");  
  123.                     "Content-Disposition:    
  124.                                "\";     
  125.                              "\"\r\n");  
  126.                     "Content-Type:"    "\r\n\r\n");  
  127.   
  128.                       
  129.   
  130.                        new   
  131.                             new   
  132.                     int   0;  
  133.                     byte[]   new byte[1024];  
  134.                     while     1)   
  135.                          0,   
  136.                       
  137.                       
  138.                   
  139.               
  140.   
  141.             byte[]   "\r\n--"    "--\r\n").getBytes();  
  142.               
  143.               
  144.               
  145.   
  146.             //   
  147.                new   
  148.                new new   
  149.                       
  150.                null;  
  151.             while     null)   
  152.                 "\n");  
  153.               
  154.                 
  155.               
  156.               null;  
  157.          catch     
  158.             "发送POST请求出错。"    
  159.               
  160.          finally   
  161.             if   null)   
  162.                   
  163.                   null;  
  164.               
  165.           
  166.         return   
  167.       
  168.   
  169. }  

| Back |