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();
    			}
    		}
    
    	}
    
    }
    
  • 相关阅读:
    EntityFramework中的线程安全,又是Dictionary
    记一次w3wp占用CPU过高的解决过程(Dictionary和线程安全)
    这一个月
    使用Nginx解决IIS绑定域名导致应用程序重启的问题
    Bootstrap for MVC:Html.Bootstrap().TextBoxFor(model=>model.Name)
    Orchard 刨析:Logging
    Orchard 刨析:Caching
    Orchard 刨析:前奏曲
    Orchard 刨析:导航篇
    数据集
  • 原文地址:https://www.cnblogs.com/gmhappy/p/11863975.html
Copyright © 2011-2022 走看看