1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package com.travel.controller;
- import cn.hutool.core.date.DateUtil;
- import com.travel.util.ossFileUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.CrossOrigin;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.multipart.MultipartFile;
- import java.text.SimpleDateFormat;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- /**
- * 上传接口
- */
- @Controller
- @RequestMapping("upload")
- @Slf4j
- @CrossOrigin
- public class UploadController {
- /**
- * 上传
- * @param file 文件
- * @return 返回结果
- */
- @RequestMapping(value = "/uploadFile")
- @ResponseBody
- public Map<String, Object> upload(MultipartFile file) {
- Map<String, Object> map = new HashMap<>();
- if (!file.isEmpty()) {
- try {
- SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
- String suffix = file.getOriginalFilename()
- .substring(file.getOriginalFilename().lastIndexOf("."));
- // 生成文件名称
- String nameSuffix = file.getOriginalFilename()
- .substring(0, file.getOriginalFilename().lastIndexOf("."))
- .replaceAll(" ", "_").replaceAll(",", "")
- + format.format(DateUtil.date())
- + new Random().nextInt(1000) + suffix;
- //上传原始图片到阿里云
- return ossFileUtil.uploadAliyun(file, nameSuffix);
- } catch (Exception e) {
- log.error("上传附件错误" + e.getMessage());
- map.put("erroMsg", "系统未知错误");
- map.put("msg", "500");
- }
- } else {
- map.put("msg", "500");
- map.put("erroMsg", "文件不能为空");
- }
- return map;
- }
- /**
- * 删除文件
- * @param fileName 文件名称
- * @return 返回结果
- */
- @RequestMapping(value = "/delUpload")
- @ResponseBody
- public Map<String, Object> delUpload(String fileName) {
- Map<String, Object> map = new HashMap<>();
- if (!fileName.isEmpty()) {
- try {
- //删除文件
- return ossFileUtil.delUploadAliyun(fileName);
- } catch (Exception e) {
- log.error("上传附件错误" + e.getMessage());
- map.put("erroMsg", "系统未知错误");
- map.put("msg", "500");
- }
- } else {
- map.put("msg", "500");
- map.put("erroMsg", "文件不能为空");
- }
- return map;
- }
- }
|