1.maven依赖

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

2.代码实现

import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; public class HttpClientUpload { public static void main(String[] args) {
// 将 fileUrl 上传到 url中,上传成功后,返回 下载地址
String url = "http://127.0.0.1:8080/Joe/upload";
String fileUrl = "C:/abc.txt";
try {
System.out.println(post(url, "file", new File(fileUrl)));
} catch (Exception e) {
e.printStackTrace();
}
} /**
* post:(上传).
*
* @author Joe Date:2017年9月11日下午5:37:46
* @param serverUrl
* @param fileParamName
* @param file
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String serverUrl, String fileParamName, File file)
throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(serverUrl);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 上传的文件
builder.addBinaryBody(fileParamName, file);
HttpEntity httpEntity = builder.build();
httpPost.setEntity(httpEntity);
// 请求获取数据的超时时间 、 设置从connect
// Manager获取Connection超时时间(因为目前版本是可以共享连接池的)、设置连接超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000).setConnectionRequestTimeout(3000)
.setConnectTimeout(10000).build();
httpPost.setConfig(requestConfig);
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpPost);
return EntityUtils.toString(response.getEntity());
}
}

HttpClient(五)-- 模拟表单上传文件的更多相关文章

  1. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  2. php Socket模拟表单上传文件函数_学习

    模拟上传文件的php代码 里面访问地址.主机.上传文件名.内容.分隔符可以修改   function postFile($file) {     $clf = "\r\n";   ...

  3. 使用CURL模拟表单上传文件

    //以下代码适合PHP7.x PHP5.6$file = new CURLFile('./127.zip','application/octet-stream');$file->setMimeT ...

  4. Android实现模拟表单上传

    很久以前,写过一篇关于下载的文章:基于HTTP协议的下载功能实现,今天对于Android上的文件上传,也简单的提两笔.在Android上,一般使用Http 模拟表单或者FTP来进行文件上传,使用FTP ...

  5. django 基于form表单上传文件和基于ajax上传文件

    一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...

  6. Express下使用formidable实现POST表单上传文件并保存

    Express下使用formidable实现POST表单上传文件并保存 在上一篇文章中使用formidable实现了上传文件,但没将它保存下来. 一开始,我也以为是只得到了文件的相关信息,需要用fs. ...

  7. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...

  8. vue form表单上传文件

    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js">< ...

  9. 使用form表单上传文件

    在使用form表单上传文件时候,input[type='file']是必然会用的,其中有一些小坑需要避免. 1.form的 enctype="multipart/form-data" ...

随机推荐

  1. Qt之QLocalServer

    简述 QLocalServer提供了一个基于本地socket的server. QLocalServer可以接受来自本地socket的连接.通过调用listen(),让server监听来自特定key的连 ...

  2. install ubuntu on Android mobile phone

    Android 是基于Linux内核的开源操作系统,主要用在移动设备上.当然同样是基于Linux内核的操作系统,现在支持的Android的智能手机理论来说都能运行基于Linux的操作系统,比如现在流行 ...

  3. Linux命令_磁盘管理_查看磁盘或目录的容量

    软件环境:虚拟机VM12,Linux版本 CentOS 7.3 命令 df (disk filesystem) 用于查看已挂载磁盘的总容量.使用容量.剩余容量等,可以不加任何参数,默认以KB为单位显示 ...

  4. 第三百四十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—Requests请求和Response响应介绍

    第三百四十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—Requests请求和Response响应介绍 Requests请求 Requests请求就是我们在爬虫文件写的Requests() ...

  5. Java如何使用线程异常?

    在Java编程中,如何使用线程异常? 此示例显示如何在处理线程时处理异常. package com.yiibai; class MyThread extends Thread { public voi ...

  6. 表单提交之List集合

    一.表单数据 <div class="panel panel-default"> <div class="panel-heading"> ...

  7. Vue中计算属性与class,style绑定

    var vm=new Vue({ el:'#app', data:{ a:2, }, computed:{ //这里的b是计算属性:默认getter b:{ get:function(){ retur ...

  8. 一款纯HTML+CSS+JS富文本编辑器-handyeditor

    官网:http://he.catfish-cms.com/ 修改版本(修改一些BUG和图片上传服务器 点击下载: handyeditor富文本编辑器.zip): 图片上传接口上传类型: Content ...

  9. 如何在Sql Server中读取最近一段时间的记录,比如取最近3天的或最近3个月的记录。

    如何在Sql Server中读取最近一段时间的记录,比如取最近3天的或最近3个月的记录. 主要用到DATEADD函数,下面是详细语句 取最近3天 select * from 表名where rq> ...

  10. MSM--Memcached_Session_Manager介绍及使用

    MSM--Memcached_Session_Manager介绍及使用 http://www.iteye.com/topic/1125301 我们都知道对于一些大型的web2.0的网站,在正式部署时一 ...