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();
    			}
    		}
    
    	}
    
    }
    
  • 相关阅读:
    FTP解决办法:服务器发回了不可路由的地址。使用服务器地址代替。
    隐藏显示终端的光标(shell echo,linux c printf)
    FTP服务2种工作方式详解,PORT方式和PASV方式,(即主动模式和被动模式)
    XP使用VNC远程桌面CentOS 6(原创,备忘)
    LINUX命令行技巧
    FTP服务器(vsftpd)配置随笔
    更快、更强 64位编程的三十二条军规
    MFC 程序入口和执行流程
    css实现居中
    treeview控件显示指定目录下的目录和文件
  • 原文地址:https://www.cnblogs.com/gmhappy/p/11863975.html
Copyright © 2011-2022 走看看