常量配置
// 文件上传路径
@Value(“${upload.path:/opt/upload}”)
private String uploadPath;
上传
附件相关实体类
@Data
public class Attachment ... {
private Long id;
// 附件名称
private String name;
// 附件路径
private String path;
// 附件绝对路径
private String fullPath;
// 附件大小
private Long fileSize;
// 附件类型
private String contentType;
// 附件扩展名
private String ext;
...
}
1.MultipartFile 上传
public void uploadFile(MultipartFile file) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String originalFileName = file.getOriginalFilename();
String ext = originalFileName.substring(originalFileName.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + ext;
String path = sdf.format(new Date()) + "/";
String fullPath = uploadPath + path;
// 得到文件保存的位置和文件名
File dest = new File(fullPath);
if (!dest.exists()) {
dest.mkdirs();
}
try {
// 上传
file.transferTo(new File(fullPath + newFileName));
// TODO 将文件信息记录到数据库中
} catch(Exception e) {
// ERROR
}
}
2.byte数组上传
public void uploadFile(byte[] fileBytes, String ext) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fileName = UUID.randomUUID().toString().replaceAll("-", "") + ext;
String path = sdf.format(new Date()) + "/";
String fullPath = uploadPath + path;
// 得到文件保存的位置和文件名
File dest = new File(fullPath);
if (!dest.exists()) {
dest.mkdirs();
}
try {
// 上传
File file = new File(fullPath, fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(fileBytes);
bos.close();
// TODO 将文件信息记录到数据库中
} catch(Exception e) {
// ERROR
}
}
下载/预览
/**
* @param fileId 附件ID
* @param downloadType 下载类型:download 下载 view 预览
*/
public static void fileDownload(HttpServletRequest request, HttpServletResponse response, Long fileId, String downloadType) {
response.reset();
// TODO 先根据fileId查询附件 attachment
.....
// 拿到文件名
String fileName = attachment.getName();
String userAgent = request.getHeader("User-Agent");
// firefox判断
if ("Firefox".contains(userAgent)) {
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1");
} else {
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
}
if ("download".equals(downloadType)) {
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
} else {
response.addHeader("Content-Disposition", "filename=" + fileName);
}
// 附件类型
String contentType = attachment.getContentType();
if (StringUtils.isNotBlank(contentType )) {
response.setContentType(contentType);
}
response.setHeader("Accept-Ranges", "bytes");
// 附件绝对路径
String fullPath = attachment.getFullPath();
fileToResponse(response, fullPath);
}
public static void fileToResponse(HttpServletResponse response, String fullPath) {
InputStream inputStream = null;
try{
inputStream = new FileInputStream(fullPath);
byte[] buf = new byte[1024 * 10];
ServletOutputStream outputStream = response.getOutputStream();
int readLength;
while((( readLength = inputStream.read(buf)) != -1 )) {
outputStream.write(buf, 0, readLength);
}
outputStream.flush();
} catch(Exception e) {
// ERROR
} finally {
...
inputStream.close();
...
}
}