主要的是设置response.addHeader,直接上代码
public void download2(HttpServletResponse response, String bucketName, String minFileName) {
InputStream fileInputStream = null;
//获取文件后缀
String fileType = minFileName.substring(minFileName.indexOf(".") + 1);
try {
//mp4视频
if("mp4".equals(fileType)){
//我这里的访问路径是http开头的(minio文件服务器)
URL url=new URL(“文件路径”);
URLConnection connection = url.openConnection();
fileInputStream = connection.getInputStream();
response.addHeader("Content-Type","video/mp4;charset=UTF-8");
}else {
//我这里是minio文件服务器获取流
fileInputStream = client.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(minFileName).build());
if ("jpg".equals(fileType) || "png".equals(fileType) || "gif".equals(fileType)) {
response.setContentType("image/" + fileType);
} else {
//如果是其他方式进行下载操作
String filename = URLEncoder.encode(minFileName.substring(minFileName.indexOf("-") + 1),"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
}
}
//流转换
IOUtils.copy(fileInputStream, response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
if (Objects.nonNull(fileInputStream)) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
response.addHeader()参数有哪些,该博主地址有统计https://blog.csdn.net/weixin_41926301/article/details/83827032



