You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.zh.project0512.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.zh.project0512.service.IUserService;
import com.zh.project0512.utils.AliyunOss;
import com.zh.project0512.utils.FileTypeUtil;
import com.zh.project0512.utils.JSONResult;
import com.zh.project0512.utils.result.HttpStatusEnum;
import com.zh.project0512.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* <p>
* 前端控制器
* </p>
*
* @author zh
* @since 2022-05-12
*/
@Tag(name = "工具中心")
@RestController
@RequestMapping("/util")
public class UtilsController {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Autowired
private AliyunOss aliyunOss;
@Value("${img_upload_path}")
private String path;
@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,"请选择文件");
}
String fileName = file.getOriginalFilename();//文件名
// fileName=name;//图片名
// String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
//String path = System.getProperty("user.home"); //文件存储位置 我放在了我的项目下
//获取jar包所在目录
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
//在jar包所在目录下生成一个upload文件夹用来存储上传的图片
String dirPath = jarF.getParentFile().toString()+"/upload/";
System.out.println(dirPath);
File dest = new File(dirPath+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("fileType", FileTypeUtil.getcontentType((fileName.substring(fileName.lastIndexOf(".")))));
res.put("fileTypeNum", FileTypeUtil.getcontentTypeNum((fileName.substring(fileName.lastIndexOf(".")))));
return Result.success(res,"上传完成");
} catch (IOException e) {
e.printStackTrace();
}
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)));
Result result = Result.success("上传成功!");
return result;
} catch (Exception e) {
e.printStackTrace();
return Result.fail(HttpStatusEnum.NOT_FOUND);
}
}
// @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();
//普通getset
stringOps.set("name", "hello");
System.out.println(stringOps.get("name"));
}
@Scheduled(fixedRate = 10000) //每10秒执行一次
// @Scheduled(cron = "0 0 0 * * ?")
public void updDailyRank() {
System.out.println("sss");
}
}