zoukankan      html  css  js  c++  java
  • Java httpClint实现文件上传

    Maven依赖

    <dependency>
    	<groupId>org.apache.httpcomponents</groupId>
    	<artifactId>httpclient</artifactId>
    	<version>4.5.3</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.httpcomponents</groupId>
    	<artifactId>httpmime</artifactId>
    	<version>4.5.3</version>
    </dependency>

    代码示例

    import java.io.File;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    
    public class Test {
    
    	public static void main(String[] args) {
    		CloseableHttpClient httpClient = null;
    		CloseableHttpResponse response = null;
    
    		try {
    			httpClient = HttpClientBuilder.create().build();
    			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000000)
    					.build();
    			HttpPost httpPost = new HttpPost("http://192.168.3.56:8080/facesearch/image");
    			httpPost.setConfig(requestConfig);
    			MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    			multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
    
    			File file = new File("D:\Picture\1552288957419.jpg");
    
    			multipartEntityBuilder.addBinaryBody("file", file);
    			HttpEntity httpEntity = multipartEntityBuilder.build();
    			httpPost.setEntity(httpEntity);
    
    			response = httpClient.execute(httpPost);
    			// 获取响应对象
    			HttpEntity resEntity = response.getEntity();
    			if (resEntity != null) {
    				// 打印响应长度
    				System.out.println("Response content length: " + resEntity.getContentLength());
    				// 打印响应内容
    				System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
    			}
    
    			// 销毁
    			EntityUtils.consume(resEntity);
    		} catch (
    
    		Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (response != null) {
    					response.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    
    			try {
    				if (httpClient != null) {
    					httpClient.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    
    	}
    
    }
    
  • 相关阅读:
    [转] Optimizely:在线网站A/B测试平台
    批处理命令——choice
    批处理命令——%0
    批处理命令——call 和 start
    批处理命令——rem 和 pause
    批处理命令——goto 和 :
    PHPCMS V9 学习总结
    PHPCMS V9 环境搭建
    批处理命令——echo 和 @
    利用Qt Assistant 定制帮助文档
  • 原文地址:https://www.cnblogs.com/gmhappy/p/11863975.html
Copyright © 2011-2022 走看看