获取post请求中的文件流
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
String uri = "http://xxxx.xx.xx.xx:xxxx/materialManageHB.do?method=downFileHB";
HttpPost httppost = new HttpPost(uri);
//设置请求参数
String params = "xxxxxxxxxxxxxxx";
httppost.setEntity(new StringEntity(params, ContentType.create("application/x-www-form-urlencoded")));
//执行请求
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
//获取返回的输入流
InputStream inputStream = resEntity.getContent();
File file = new File("c:/d/dd.pdf");
if (file.exists()) {
file.delete();
}
byte[] bytes = IOUtils.toByteArray(inputStream);
FileUtils.writeByteArrayToFile(file, bytes);
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
直接通过url获取文件流
public static void main(String args[]) throws IOException {
String url1 = "http://xxxx.xx.xx.xx:xxxx/temp/5be6a4e0-a10d-45b2-be39-865075a62337.pdf";
URL url = new URL(url1);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream("D:/111.pdf");
byte bf[] = new byte[1024];
int length = 0;
while ((length = is.read(bf, 0, 1024)) != -1) {
os.write(bf, 0, length);
}
is.close();
os.close();
}