HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/upload");
File file = new File("/path/to/myfile");
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length());
httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
使用InputStreamEntity 边读取边上传文件
最新推荐文章于 2022-08-07 02:20:25 发布
本文介绍如何使用Java的HttpClient进行文件上传操作。通过创建HttpClient实例,并设置HttpPost请求到指定URL,然后利用FileInputStream读取本地文件,将文件内容封装为InputStreamEntity进行发送。最后消费响应内容并关闭连接。
417

被折叠的 条评论
为什么被折叠?



