添加文件缓存

This commit is contained in:
JiaXing_Cheng@163.com
2021-10-15 17:27:07 +08:00
parent 469548b972
commit 98244fc048
8 changed files with 449 additions and 194 deletions

View File

@ -2,6 +2,7 @@ package com.ruoyi.productionManager.service;
import java.util.List;
import com.ruoyi.productionManager.domain.StandardInfo;
import org.springframework.web.multipart.MultipartFile;
/**
* 试验标准管理Service接口
@ -58,4 +59,11 @@ public interface IStandardInfoService
* @return 结果
*/
public int deleteStandardInfoByStandardId(Long standardId);
/**
* 上传文件
* @param file
* @return
*/
public String uploadFile(MultipartFile file);
}

View File

@ -1,12 +1,24 @@
package com.ruoyi.productionManager.service.impl;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import io.netty.handler.codec.base64.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.productionManager.mapper.StandardInfoMapper;
import com.ruoyi.productionManager.domain.StandardInfo;
import com.ruoyi.productionManager.service.IStandardInfoService;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
/**
* 试验标准管理Service业务层处理
@ -20,6 +32,9 @@ public class StandardInfoServiceImpl implements IStandardInfoService
@Autowired
private StandardInfoMapper standardInfoMapper;
@Autowired
private RedisCache redisCache;
/**
* 查询试验标准管理
*
@ -93,4 +108,38 @@ public class StandardInfoServiceImpl implements IStandardInfoService
{
return standardInfoMapper.deleteStandardInfoByStandardId(standardId);
}
@Override
public String uploadFile(MultipartFile file) {
try {
String filePath = RuoYiConfig.getUploadPath();
IdUtils.fastUUID();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String name ="";
for(String s:fileName.split("/",9)){
System.out.println("path>>>"+s);
name = s;
}
Byte[] data = new Byte[file.getResource().getInputStream().available()];
redisCache.setCacheObject(Constants.UPLOAD_FILE+name,
data, 30, TimeUnit.MINUTES);
// 取出缓存数据
Byte[] bytes = redisCache.getCacheObject(Constants.UPLOAD_FILE+name);
return fileName;
}catch (Exception e){
throw new ServiceException("上传失败:" + e.getMessage());
}
}
/**
* 设置cache key
*
* @param fileName 参数键
* @return 缓存键key
*/
private String getCacheKey(String fileName)
{
return Constants.UPLOAD_FILE + fileName;
}
}