中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

利用 HttpClient 上傳文件

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
 最近的工作需要把從網(wǎng)絡(luò)上抓取的圖片批量上傳到服務(wù)器,文件上傳用的是Apache HttpClient 4.3,記錄一下以便以后查閱!

代碼如下:

    /** 
     * Example how to use multipart/form encoded POST request. 
     */  
    public class ClientMultipartFormPost {  
      
        public static void main(String[] args) throws Exception {  
            if (args.length != 1)  {  
                System.out.println("File path not given");  
                System.exit(1);  
            }  
            CloseableHttpClient httpclient = HttpClients.createDefault();  
            try {  
                HttpPost httppost = new HttpPost("http://localhost:8080" +  
                        "/servlets-examples/servlet/RequestInfoExample");  
      
                FileBody img = new FileBody(new File(args[0]));  
                StringBody filename = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);  
                StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);  
      
                HttpEntity reqEntity = MultipartEntityBuilder.create()  
                        .addPart("img", img)  
                        .addPart("filename", filename)  
                        .addPart("comment", comment)  
                        .build();  
      
      
                httppost.setEntity(reqEntity);  
      
                System.out.println("executing request " + httppost.getRequestLine());  
                CloseableHttpResponse response = httpclient.execute(httppost);  
                try {  
                    System.out.println("----------------------------------------");  
                    System.out.println(response.getStatusLine());  
                    HttpEntity resEntity = response.getEntity();  
                    if (resEntity != null) {  
                        System.out.println("Response content length: " + resEntity.getContentLength());  
                    }  
                    EntityUtils.consume(resEntity);  
                } finally {  
                    response.close();  
                }  
            } finally {  
                httpclient.close();  
            }  
        }  
      
    }  

HttpClient的更多用法可參考官方文檔:https://hc.apache.org/httpcomponents-client-4.3.x/examples.html

標(biāo)簽: 代碼 服務(wù)器 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:PyQt寫的圖片瀏覽器

下一篇:Java判斷圖片格式的代碼