问题1:为什么我下载的图片只有半张。或者我下的文件打开报文件损坏。
原因:你可能采用的是写法是:https://www.jianshu.com/p/0951086020d9
byte[] buffer = new byte[inputStream.available() 这个是罪魁祸首。这个返回的仅仅是可读取字节数。并不一定是我们文件的大小。所以我们应该采用的方式是:
byte[] buffer = new byte[inputStream.available()];
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
int n;
while ((n = inputStream.read(buffer)) != -1) {
toClient.write(buffer, 0, n);
}
问题2:为什么我下载的图片的名字英文名是好的,中文名是下滑线
原因是:你没有按照正确的格式返回,就是数据的字符格式没有设置正确。
解决方案是:
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileBean.getName(),"UTF-8"));
问题2:为什么我下载的图片的名字英文名是好的,中文名是一个加密后的名字
原因是:恭喜你,这不是你的原因,前端同学得对该数据进行转义
这个我写的几种下载方式:
/**
* 下载文件
*
* @param fileBean
* @return
*/
@RequestMapping(path = "/download2", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
public HttpServletResponse download2(@RequestBody FileBean fileBean,HttpServletResponse response) {
try {
//入参 桶名,文件名
ObjectRequest objectRequest = new ObjectRequest(BUCKET_NAME,fileBean.getFilePath());
//下载的文件流
InputStream inputStream = mssClientObjectService.getObject(objectRequest);
//具体的业务操作
//2、创建字节输出流
byte[] b = new byte[1024];
//2、创建字节输出流
OutputStream sos = response.getOutputStream();
int len ;
while((len=inputStream.read(b)) != -1){
sos.write(b,0 , len);
}
sos.close();
inputStream.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(fileBean.getName().getBytes()));
response.addHeader("Content-Length",
"" + b.length);
response.setContentType("application/octet-stream");
} catch (AmazonClientException | IOException e) {
log.error("DPL:down File error :{}",fileBean,e);
}
return response;
}
/**
* 下载文件
*
* @param fileBean
* @return
*/
@RequestMapping(path = "/download3", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
public HttpServletResponse download3(@RequestBody FileBean fileBean,HttpServletResponse response) {
try {
//入参 桶名,文件名
ObjectRequest objectRequest = new ObjectRequest(BUCKET_NAME,fileBean.getFilePath());
//下载的文件流
InputStream inputStream = mssClientObjectService.getObject(objectRequest);
//2、创建字节输出流
byte[] b = new byte[inputStream.available()];
//2、创建字节输出流
OutputStream sos = new BufferedOutputStream(response.getOutputStream());
int n;
while((n = inputStream.read(b)) != -1){
sos.write(b,0,n);
}
inputStream.close();
sos.flush();
sos.close();
// 设置response的Header
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(fileBean.getName().getBytes()));
response.addHeader("Content-Length",
"" + b.length);
response.setContentType("application/octet-stream");
} catch (AmazonClientException | IOException e) {
log.error("DPL:down File error :{}",fileBean,e);
}
return response;
}
/**
* 下载文件
*
* @param fileBean
* @return
*/
@RequestMapping(path = "/download", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
public HttpServletResponse download(@RequestBody FileBean fileBean,HttpServletResponse response) {
try {
//入参 桶名,文件名
ObjectRequest objectRequest = new ObjectRequest(BUCKET_NAME,fileBean.getFilePath());
//下载的文件流
InputStream inputStream = mssClientObjectService.getObject(objectRequest);
//具体的业务操作
byte[] buffer = toByteArray(inputStream);
inputStream.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileBean.getName(),"UTF-8"));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (AmazonClientException | IOException e) {
log.error("DPL:down File error :{}",fileBean,e);
}
return response;
}
private byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[input.available()];
int n;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
/**
* 下载文件
*
* @param fileBean
* @return
*/
@RequestMapping(path = "/download4", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public HttpServletResponse download4(@RequestBody FileBean fileBean, HttpServletResponse response) {
try {
// 清空response
response.reset();
//入参 桶名,文件名
ObjectRequest objectRequest = new ObjectRequest(BUCKET_NAME, fileBean.getFilePath());
//下载的文件流
InputStream inputStream = mssClientObjectService.getObject(objectRequest);
//具体的业务操作
byte[] buffer = new byte[inputStream.available()];
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
int n;
while ((n = inputStream.read(buffer)) != -1) {
toClient.write(buffer, 0, n);
}
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileBean.getName().getBytes()));
response.setContentType("application/octet-stream");
inputStream.close();
toClient.flush();
toClient.close();
} catch (AmazonClientException | IOException e) {
log.error("DPL:down File error :{}", fileBean, e);
}
return response;
}