博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient4.x模拟上传文件
阅读量:6979 次
发布时间:2019-06-27

本文共 1543 字,大约阅读时间需要 5 分钟。

hot3.png

public static void main(String[] args) throws Exception {        if (args.length != 1)  {            System.out.println("File path not given");            System.exit(1);        }        CloseableHttpClient httpclient = HttpClients.createDefault();        try {            HttpPost httppost = new HttpPost("http://localhost:8080" +                    "/servlets-examples/servlet/RequestInfoExample");            FileBody bin = new FileBody(new File(args[0]));            StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);            HttpEntity reqEntity = MultipartEntityBuilder.create()                    .addPart("bin", bin)                    .addPart("comment", comment)                    .build();            httppost.setEntity(reqEntity);            System.out.println("executing request " + httppost.getRequestLine());            CloseableHttpResponse response = httpclient.execute(httppost);            try {                System.out.println("----------------------------------------");                System.out.println(response.getStatusLine());                HttpEntity resEntity = response.getEntity();                if (resEntity != null) {                    System.out.println("Response content length: " + resEntity.getContentLength());                }                EntityUtils.consume(resEntity);            } finally {                response.close();            }        } finally {            httpclient.close();        }    }

转载于:https://my.oschina.net/cshuangxi/blog/262354

你可能感兴趣的文章
tomcat的startup.bat闪退问题解决
查看>>
你不怕他离职吗?
查看>>
使用Linux进行缓冲区溢出实验的配置记录
查看>>
JavaScript-数据引用类型对象
查看>>
mysql删除开放用户权限
查看>>
17.08.17
查看>>
枚举算法
查看>>
April Fools Contest 2018
查看>>
关于flume配置加载(二)
查看>>
delphi 字符串string转流TStream
查看>>
Lync 小技巧-51-Lync 2013-不加域-客户端-1-下载-证书-信任链
查看>>
awk数组命令经典生产实战应用拓展
查看>>
配套自测连载(二)
查看>>
linux下set和eval的使用小案例精彩解答
查看>>
为什么很多人努力了却死一地
查看>>
开放产品开发(OPD):Archi 汉化工具下载
查看>>
VS code for python开发利器
查看>>
高性能的MySQL(1)锁和MVCC
查看>>
如何用VDP备份虚拟机
查看>>
虚拟机安装 Windows 10 9926 预览版 “准备就绪”...... 故障
查看>>