文件相关接口

常量配置

// 文件上传路径
@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();
		...
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值