Java 后台POST模拟文件上传

概述

  废话不多说,直接撸代码

代码

  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);
   }

 

转载于:https://www.cnblogs.com/xibei666/p/9959160.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值