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();
    			}
    		}
    
    	}
    
    }
    
  • 相关阅读:
    BZOJ 1257 余数之和
    BZOJ 1251 序列终结者
    BZOJ 2716 [Violet 3]天使玩偶
    BZOJ 2648 SJY摆棋子
    HDU 1007 Quoit Design
    BZOJ 3504 危桥
    BZOJ 1877 晨跑
    玩转Web之SSH--Heibernate (一)---第一个demo
    网页信息抓取进阶 支持Js生成数据 Jsoup的不足之处
    2013-09-16 构建C1000K的服务器(1) – 基础
  • 原文地址:https://www.cnblogs.com/gmhappy/p/11863975.html
Copyright © 2011-2022 走看看