IO输出
下载到本地 (片段)
private void downLoad(String url,String imgName) throws Exception {
HttpResponse httpResponse = httpUtils.sendGetResp(url, "utf-8",null);
InputStream inputStream = httpResponse.getEntity().getContent();
String name = "/data/images/" + imgName;
File file = new File(name);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
byte[] data = new byte[1024];
int len = 0;
while ((len = inputStream.read(data)) != -1) {
fos.write(data, 0, len);
}
FileUtils.close(inputStream,fos);
}
http响应文件(片段)
InputStream download = MinIoUtils.download(personPath +path, minIoUrl, accessKey, secretKey, bucketName);
ServletOutputStream outputStream = null;
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename="
+ path);
try {
outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=download.read(b)) != -1){
outputStream.write(b, 0, len);
}
outputStream.flush();
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.close();
download.close();
} catch (IOException e) {
e.printStackTrace();
}
}