|
@@ -1,131 +0,0 @@
|
|
|
-package com.ruoyi.file.service;
|
|
|
-
|
|
|
-import com.ruoyi.file.enums.AppHttpCodeEnum;
|
|
|
-import com.ruoyi.file.mapper.UploadFileMapper;
|
|
|
-import com.ruoyi.file.utils.ResponseResult;
|
|
|
-import com.ruoyi.file.utils.Tools;
|
|
|
-import org.apache.commons.collections4.CollectionUtils;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
-import org.apache.commons.lang3.RandomStringUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.*;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-@Service
|
|
|
-public class UploadFileServiceImpl implements UploadFileService {
|
|
|
-
|
|
|
- private static final String LOCAL_HOST = "https://file.usky.cn/static/";
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private UploadFileMapper uploadFileMapper;
|
|
|
-
|
|
|
- @Override
|
|
|
- @Transactional
|
|
|
- public ResponseResult upload(MultipartFile multipartFile, String bussinessId) throws Exception {
|
|
|
- if (multipartFile.isEmpty()) {
|
|
|
- return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"上传文件为空");
|
|
|
- }
|
|
|
- UploadFile uploadFile = new UploadFile();
|
|
|
- // 保存原文件名称,文件列表展示需要用到
|
|
|
- uploadFile.setFileName(multipartFile.getOriginalFilename());
|
|
|
- // 生成系统文件名称,不可重复,防止同名文件上传覆盖问题
|
|
|
- String name = RandomStringUtils.randomAlphanumeric(32) + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")).toLowerCase();
|
|
|
- uploadFile.setName(name.substring(0, name.lastIndexOf(".")));
|
|
|
- uploadFile.setFileSuffix(multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")).toLowerCase());
|
|
|
- // 判断文件类型
|
|
|
- int fileType = Tools.fileType(name);
|
|
|
- uploadFile.setType(fileType);
|
|
|
- uploadFile.setDomain(LOCAL_HOST + "resource/files/");
|
|
|
- //这种方式比类型强转效率更高
|
|
|
- String pathExt = System.currentTimeMillis()+"";
|
|
|
- uploadFile.setPath(LOCAL_HOST + "resource/files/" + pathExt);
|
|
|
- uploadFile.setPathExt(pathExt);
|
|
|
- // 获取文件大小
|
|
|
- uploadFile.setSize((int)multipartFile.getSize());
|
|
|
- uploadFile.setBussinessId(bussinessId);
|
|
|
- uploadFile.setStatus(1);
|
|
|
- uploadFile.setCreateTime(LocalDateTime.now());
|
|
|
- uploadFile.setId(Tools.getCode32());
|
|
|
- uploadFileMapper.insert(uploadFile);
|
|
|
-
|
|
|
- // 将文件保存到本目录/resources/files/下
|
|
|
- // DateUtil.today()得到得是当天日期如:20230715,这个会在/resources/files/下再以日期生成一层目录
|
|
|
- File newFile = new File("./resources/files/"+ pathExt + "/" + name);
|
|
|
- // 保证这个文件的父文件夹必须要存在
|
|
|
- if (!newFile.getParentFile().exists()) {
|
|
|
- newFile.getParentFile().mkdirs();
|
|
|
- }
|
|
|
- newFile.createNewFile();
|
|
|
- // 将文件内容写入到这个文件中
|
|
|
- InputStream is = multipartFile.getInputStream();
|
|
|
- FileOutputStream fos = new FileOutputStream(newFile);
|
|
|
- try {
|
|
|
- int len;
|
|
|
- byte[] buf = new byte[1024];
|
|
|
- while ((len = is.read(buf)) != -1) {
|
|
|
- fos.write(buf, 0, len);
|
|
|
- }
|
|
|
- } finally {
|
|
|
- // 关流顺序,先打开的后关闭
|
|
|
- fos.close();
|
|
|
- is.close();
|
|
|
- }
|
|
|
-
|
|
|
- // 返回文件信息给前端
|
|
|
- Map resultMap = new HashMap();
|
|
|
- resultMap.put("id", uploadFile.getId());
|
|
|
- resultMap.put("path", uploadFile.getPath());
|
|
|
- return ResponseResult.okResult(resultMap);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ResponseResult download(HttpServletResponse response, String id) throws Exception {
|
|
|
- List<UploadFile> uploadFiles = uploadFileMapper.selectOne(id, null, 1);
|
|
|
- if (CollectionUtils.isEmpty(uploadFiles)) {
|
|
|
- return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "查询文件为空");
|
|
|
- }
|
|
|
-
|
|
|
- // 去./resources/files/目录下取出文件
|
|
|
- File downloadFile = new File("./resources/files/" +uploadFiles.get(0).getPathExt() + "/" + uploadFiles.get(0).getName() + uploadFiles.get(0).getFileSuffix());
|
|
|
- if (!downloadFile.exists() || downloadFile.length() == 0) {
|
|
|
- return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "查询文件为空");
|
|
|
- }
|
|
|
-
|
|
|
- InputStream is = null;
|
|
|
- OutputStream os = null;
|
|
|
- try {
|
|
|
- // 判断是否是图片,如果是图片加上 response.setContentType("image/jpeg"),这样就可以直接在浏览器打开而不是下载
|
|
|
- if (uploadFiles.get(0).getType() == 1) {
|
|
|
- response.setContentType("image/jpeg");
|
|
|
- }
|
|
|
- response.addHeader("Content-Length", "" + downloadFile.length());
|
|
|
- is = new FileInputStream(downloadFile);
|
|
|
- os = response.getOutputStream();
|
|
|
- IOUtils.copy(is, os);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("下载文件发生异常", e);
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (os != null) {
|
|
|
- os.flush();
|
|
|
- os.close();
|
|
|
- }
|
|
|
- if (is != null) {
|
|
|
- is.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("关闭流发生异常", e);
|
|
|
- }
|
|
|
- }
|
|
|
- return ResponseResult.okResult("下载成功");
|
|
|
- }
|
|
|
-}
|