|
|
|
@ -24,6 +24,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneOffset;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
@ -40,23 +43,22 @@ public class UtilsController {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisTemplate<String,String> redisTemplate;
|
|
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
@Autowired
|
|
|
|
|
private AliyunOss aliyunOss;
|
|
|
|
|
|
|
|
|
|
@Value("${web.uploadPath}")
|
|
|
|
|
private String path;
|
|
|
|
|
|
|
|
|
|
@Operation(summary="上传")
|
|
|
|
|
@Operation(summary = "上传")
|
|
|
|
|
@PostMapping("/upload")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public Result upload(@RequestParam("file") MultipartFile file,@RequestParam(required = false) String name) {
|
|
|
|
|
if(file.isEmpty()){
|
|
|
|
|
return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION,"请选择文件");
|
|
|
|
|
public Result upload(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name) {
|
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
|
return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "请选择文件");
|
|
|
|
|
}
|
|
|
|
|
String fileName = file.getOriginalFilename();//文件名
|
|
|
|
|
// fileName=name;//图片名
|
|
|
|
|
// String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
|
|
|
|
|
String newFileName = UUID.randomUUID().toString().replace("-", "") + LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli() + fileName.substring(fileName.lastIndexOf("."));
|
|
|
|
|
//String path = System.getProperty("user.home"); //文件存储位置 我放在了我的项目下
|
|
|
|
|
// //获取jar包所在目录
|
|
|
|
|
// ApplicationHome h = new ApplicationHome(getClass());
|
|
|
|
@ -66,31 +68,31 @@ public class UtilsController {
|
|
|
|
|
// System.out.println(dirPath);
|
|
|
|
|
// System.out.println(path);
|
|
|
|
|
// File dest = new File(dirPath+fileName);
|
|
|
|
|
File dest = new File(path+"/"+fileName);
|
|
|
|
|
File dest = new File(path + "/" + fileName);
|
|
|
|
|
if (!dest.getParentFile().exists()) {
|
|
|
|
|
dest.getParentFile().mkdirs();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
file.transferTo(dest);
|
|
|
|
|
JSONObject res = new JSONObject();
|
|
|
|
|
res.put("fileName",fileName);
|
|
|
|
|
res.put("fileUrl","/upload/"+fileName);
|
|
|
|
|
res.put("fileName", fileName);
|
|
|
|
|
res.put("fileUrl", "/upload/" + newFileName);
|
|
|
|
|
res.put("fileType", FileTypeUtil.getcontentType((fileName.substring(fileName.lastIndexOf(".")))));
|
|
|
|
|
res.put("fileTypeNum", FileTypeUtil.getcontentTypeNum((fileName.substring(fileName.lastIndexOf(".")))));
|
|
|
|
|
return Result.success(res,"上传完成");
|
|
|
|
|
return Result.success(res, "上传完成");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION,"上传失败");
|
|
|
|
|
return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "上传失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/uploadImg")
|
|
|
|
|
public Result uploadImg(@RequestParam("file") MultipartFile file, @RequestParam(value = "name", defaultValue = "name") String name) {
|
|
|
|
|
try {
|
|
|
|
|
String homeImage = aliyunOss.checkImage(file);//此处是调用上传服务接口
|
|
|
|
|
String fileUrl = aliyunOss.getImgUrl(homeImage);
|
|
|
|
|
String fileType = FileTypeUtil.getcontentType((fileUrl.substring(fileUrl.lastIndexOf("."))));
|
|
|
|
|
System.out.println( fileType == "video/mp4"?1:(fileType == "image/jpg"?2:(fileType == "text/plain"?3:0)));
|
|
|
|
|
String fileUrl = aliyunOss.getImgUrl(homeImage);
|
|
|
|
|
String fileType = FileTypeUtil.getcontentType((fileUrl.substring(fileUrl.lastIndexOf("."))));
|
|
|
|
|
System.out.println(fileType == "video/mp4" ? 1 : (fileType == "image/jpg" ? 2 : (fileType == "text/plain" ? 3 : 0)));
|
|
|
|
|
Result result = Result.success("上传成功!");
|
|
|
|
|
return result;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
@ -99,13 +101,6 @@ public class UtilsController {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @PostMapping("/add")
|
|
|
|
|
// public JSONResult add(@RequestParam String name){
|
|
|
|
|
// User u = new User();
|
|
|
|
|
// u.setName(name);
|
|
|
|
|
// userService.save(u);
|
|
|
|
|
// return JSONResult.ok();
|
|
|
|
|
// }
|
|
|
|
|
@PostMapping("/redis")
|
|
|
|
|
public void testString() {
|
|
|
|
|
ValueOperations<String, String> stringOps = redisTemplate.opsForValue();
|
|
|
|
@ -114,7 +109,7 @@ public class UtilsController {
|
|
|
|
|
System.out.println(stringOps.get("name"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Scheduled(fixedRate = 10000) //每10秒执行一次
|
|
|
|
|
@Scheduled(fixedRate = 60000) //每60秒执行一次
|
|
|
|
|
// @Scheduled(cron = "0 0 0 * * ?")
|
|
|
|
|
public void updDailyRank() {
|
|
|
|
|
System.out.println("进行中...");
|
|
|
|
|