java后台接受 input file java通过http接收文件流
转载
声明:根据http请求图片的url下载文件到本地
public void downLoadPhoto(String photoUrl)throws Exception{
//获取照片返回二进制流
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
ResponseEntity<byte[]> entity = restTemplate.exchange(photoUrl, HttpMethod.GET,new HttpEntity<>(headers), byte[].class);
byte[] body = entity.getBody();
InputStream sbs = new ByteArrayInputStream(body);
File dest = new File("/Users/qixin/Desktop/a.jpg");
if(!dest.exists()) {
dest.createNewFile();
}
InputStream in = null;
OutputStream out = null;
in = new ByteArrayInputStream(body);
out = new FileOutputStream(dest);
byte []bt = new byte[1024];
int length=0;
while( (length = in.read(bt)) !=-1) {
//一次性写入文件a中
out.write(bt,0,length);
}
if(null!=out) {
out.close();
}
}
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。