概述

  废话不多说,直接撸代码

代码

  1、引入Maven包

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

2、编写Java代码

//引入Java包
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient; public static String FilePost(String url,File value) throws Exception {
String BOUNDARY = java.util.UUID.randomUUID().toString();
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------"+BOUNDARY, Charset.defaultCharset());
//multipartEntity.addPart("key",new StringBody("123456",Charset.forName("UTF-8")));
//multipartEntity.addPart("from",new StringBody("cw",Charset.forName("UTF-8")));
multipartEntity.addPart("image_binary", new FileBody(value));
//InputStream inputStream=new FileInputStream(value);;
//inputStream=new ByteArrayInputStream("字符串转输入流".getBytes(Charset.forName("UTF-8")));
//inputStream= IOUtil.newInputStream(ByteBuffer.wrap("字符串".getBytes()));
//multipartEntity.addPart("image_binary",new InputStreamBody(inputStream,"1.jpg"));
HttpPost request = new HttpPost(url);
request.setEntity(multipartEntity);
request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------"+BOUNDARY);
//request.addHeader("Content-Type","image/jpeg"); //视情况而定 DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
System.out.println("发送消息收到的返回:"+buffer.toString());
return buffer.toString();
} public static void main(String[] args) throws Exception {
File file=new File("F:\\test\\test\\1.jpg");
String Result=FilePost("http://www.baidu.com/ocr/v2/general_ex",file);
}

Java 后台POST模拟文件上传的更多相关文章

  1. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  2. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

  3. Java 利用SWFUpload多文件上传 session 为空失效,不能验证的问题 swfUpload多文件上传

    Java 利用SWFUpload多文件上传 session 为空失效,不能验证的问题(转) 我们都知道普通的文件上传是通过表单进行文件上传的,还不能达到异步上传的目的.通过使用某些技术手段,比如jqu ...

  4. Fiddler2 模拟文件上传

    最近遇到一个需求,需要上传音频文件, 服务端使用webService 通过spring3 进行文件上传.代码完成后使用 html 通过post 方式请求接口成功了,但不知道如何使用Fiddler2工具 ...

  5. selenium+java利用AutoIT实现文件上传

    转自https://www.cnblogs.com/yunman/p/7112882.html?utm_source=itdadao&utm_medium=referral 1.AutoIT介 ...

  6. Jmeter之模拟文件上传、下载接口操作

    上周群里有位同学,问我用jmeter怎么上传文件?因好久没用jmeter了,顺便自己也复习下,现整理出来和大家分享 一.准备工作: 上传接口一个(自行开发解决了) 下载接口 ps:没有困难创造困难也要 ...

  7. java常见3种文件上传速度对比和文件上传方法详细代码

    在java里面文件上传的方式很多,最简单的依然是FileInputStream.FileOutputStream了,在这里我列举3种常见的文件上传方法代码,并比较他们的上传速度(由于代码是在本地测试, ...

  8. Java精选笔记_文件上传与下载

    文件上传与下载 如何实现文件上传 在Web应用中,由于大多数文件的上传都是通过表单的形式提交给服务器的,因此,要想在程序中实现文件上传的功能,首先得创建一个用于提交上传文件的表单页面. 为了使Serv ...

  9. java使用commons-fileupload进行文件上传

    java中使用文件上传时需要使用特定的类库,这里使用commons-files类库进行文件上传,在http://commons.apache.org/proper/commons-fileupload ...

随机推荐

  1. 51nod97B 异或约束和

    题目 定义\(f(i)\)为\(i\)的所有约数的异或和,给定 \(n(1\le n \le 10^{14})\) ,求\(f(1) xor f(2) xor f(3) xor...xor f(n)\ ...

  2. I2C(三) linux3.4(内核分析)

    目录 I2C(三) linux3.4(内核分析) (一)总线流程 bus.probe match i2c_device_probe (二)client注册 方式(一)静态加载 方式(二)指定设备 方式 ...

  3. 静态网站创建工具Docusaurus

    地址:https://docusaurus.io/docs/zh-CN/installation 安装 Docusaurus

  4. System系统类

    System系统类 : 主要的作用是用于获取系统的一个参数. System类需要掌握的方法: arraycopy(Object src, int srcPos, Object dest, int de ...

  5. python3 练手实例7 斐波那契数列

    '''a,b=0,1 x=int(input('请指定需要多少项:')) while x>0: print(b) a,b=b,a+b x-=1''' #递归 def fibo(n): if n& ...

  6. java基于redis事务的秒杀实现

    package com.vian.user.service; import org.junit.Test; import org.springframework.util.CollectionUtil ...

  7. 解决本地SqlServer无法连接远程服务器数据库,错误10060

    本地SqlServer 连不上服务器的数据库环境,错误信息如下图,折腾来折腾去,最终还是解决了 第一步 查看服务器本地端口是否已经打开,查看方法:首先向C:\Windows\System32文件夹添加 ...

  8. html-webpack-plugin详解

    引言 我们来看看主要作用: 为html文件中引入的外部资源如script.link动态添加每次compile后的hash,防止引用缓存的外部文件问题 可以生成创建html入口文件,比如单页面可以生成一 ...

  9. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165237

    2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165237 安装虚拟机 首先创建虚拟机 创建好虚拟机后,打开虚拟机进行安装.第一步选择Graphcal i ...

  10. ZOJ 4110 Strings in the Pocket (马拉车+回文串)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110 题目: BaoBao has just found two s ...