Commit 3e362662 authored by 周健威's avatar 周健威

统一数据库上传接口

parent f787fe20
......@@ -162,21 +162,14 @@ public class UploadController{
return ObjectRestResponse.succ(data);
}
// @PostMapping("/app/unauth/download")
// public byte[] downloadFile(@RequestBody String fileUrl) {
// byte[] fileBytes = null;
// try {
//
// File file = new File(fileUrl);
// InputStream inputStream = new FileInputStream(file);
// fileBytes = StreamUtils.copyToByteArray(inputStream);
// } catch (Exception e) {
// e.printStackTrace();
// }
// return fileBytes;
// }
@RequestMapping(value = "/app/unauth/admin/uploadTempcaheFile", method = RequestMethod.POST)
public ObjectRestResponse uploadTempcaheFile(
@RequestParam("file") MultipartFile file,
@RequestParam(value = "prefix",defaultValue = "upload")String prefix
)throws Exception {
FileData data = uploadService.uploadTempcaheFile(file,prefix);
return ObjectRestResponse.succ(data);
}
@RequestMapping(value = "/app/unauth/admin/upload250", method = RequestMethod.POST)
public JSONObject uploads250(
......@@ -184,13 +177,6 @@ public class UploadController{
@RequestParam(value = "prefix",defaultValue = "admin")String prefix
)throws Exception {
String contentType = file.getContentType(); //图片文件类型
// String fileName = file.getOriginalFilename(); //图片名字
/* if (!contentType.equals("image/jpeg") && !contentType.equals("image/gif")&&!contentType.equals("image/png")) {
return JsonResultUtil.createFailedResult(2001,"格式不对!!!");
}*/
// if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
// return JsonResultUtil.createFailedResult(2002,"大小超过限制!!!");
// }
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile250(file,prefix));
}
......
......@@ -45,6 +45,9 @@ public class UploadService {
@Value("${universal.gaindataPath}")
private String gaindataPath ;
@Value("${universal.tempcachePath}")
private String tempcachePath ;
@Value("${universal.videoUploadPath}")
private String videoUploadPath ;
......@@ -175,6 +178,38 @@ public class UploadService {
return fileData;
}
public FileData uploadTempcaheFile(MultipartFile file, String prefix) throws Exception{
//创建本日存放目录
DateTime now = DateTime.now();
String dirPath = "/"+prefix;
String todayStr = now.toString(GAINFILE_DATE_TIME_FORMATTER);
String redisNoKey = RedisKey.UPLOAD_FILE_NO_PREFIX + now.toString(DEFAULT_DATE_TIME_FORMATTER);
Long no = redisTemplate.opsForValue().increment(redisNoKey);
if(no.equals(1l)){
redisTemplate.expire(redisNoKey,1, TimeUnit.DAYS);
}
String noStr = todayStr + "_" + no;
String fileName = file.getOriginalFilename();
String realFileRelPath = tempcachePath + dirPath + "/" + noStr + fileName.substring(fileName.lastIndexOf("."));
String filetype = fileName.substring(fileName.lastIndexOf("."));
long fileSize = file.getSize();
double fileSizeInMB = (double) fileSize / (1024 * 1024); // 转换为 MB
double roundedFileSizeInMB = Math.round(fileSizeInMB * 100.0) / 100.0; // 保留两位小数
String fileSizeString = String.format("%.2f MB", roundedFileSizeInMB);
//文件存放路径
String filePath = baseUploadPath + realFileRelPath;
//将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
realFileRelPath=xx_url+SystemConfig.XXMP_URL+realFileRelPath;
FileData fileData = new FileData();
fileData.setFilename(fileName);
fileData.setFiletype(filetype);
fileData.setFilesize(fileSizeString);
fileData.setFileurl(realFileRelPath);
fileData.setFilepath(filePath);
return fileData;
}
// public String uploadFile250(MultipartFile file, String prefix) throws Exception {
//// // 创建本日存放目录
//// DateTime now = DateTime.now();
......
......@@ -21,10 +21,8 @@ import com.upyuns.platform.rs.website.entity.GainData;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment