| package com.xzx.gc.system.controller;  | 
|   | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.date.DateUtil;  | 
| import cn.hutool.core.io.FileUtil;  | 
| import cn.hutool.core.util.IdUtil;  | 
| import cn.hutool.core.util.StrUtil;  | 
| import com.xzx.gc.common.Result;  | 
| import com.xzx.gc.common.constant.Constants;  | 
| import com.xzx.gc.common.dto.CommonDto;  | 
| import com.xzx.gc.common.request.BaseController;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.common.utils.FileUtils;  | 
| import com.xzx.gc.entity.ResourceInfo;  | 
| import com.xzx.gc.system.mapper.ResourceInfoMapper;  | 
| import io.swagger.annotations.Api;  | 
| import io.swagger.annotations.ApiOperation;  | 
| import io.swagger.annotations.ApiParam;  | 
| import lombok.extern.slf4j.Slf4j;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.http.MediaType;  | 
| import org.springframework.web.bind.annotation.*;  | 
| import org.springframework.web.multipart.MultipartFile;  | 
|   | 
| import javax.servlet.http.HttpServletRequest;  | 
| import java.io.IOException;  | 
| import java.util.ArrayList;  | 
| import java.util.List;  | 
|   | 
| @RestController  | 
| @Api(tags = {"上传下载管理"})  | 
| @Slf4j  | 
| public class UpOrDownloadController extends BaseController {  | 
|   | 
|     @Autowired  | 
|     private BusinessUtil businessUtil;  | 
|   | 
|     @Autowired  | 
|     private ResourceInfoMapper resourceInfoMapper;  | 
|   | 
|   | 
|     @ApiOperation("文件上传")  | 
|     @PostMapping(value = {"/file/upload", "/admin/front/file/upload"})  | 
|     @ResponseBody  | 
|     public Result<List<ResourceInfo>> upload(HttpServletRequest request, @ApiParam(value = "文件类型 ,1:默认图片") @RequestParam(name = "type", defaultValue = "1") int type, @RequestParam(name = "file") MultipartFile[] files) throws IOException {  | 
|         Result result = new Result();  | 
|         if (files == null || files.length == 0) {  | 
|             result.setCode(-1);  | 
|             result.setMsg("没有需要上传的文件");  | 
|             return result;  | 
|         }  | 
|   | 
|         if (files.length > 6) {  | 
|             result.setCode(-1);  | 
|             result.setMsg("最多只能传6张图片");  | 
|             return result;  | 
|         }  | 
|   | 
|   | 
|         long sumSize = 0;  | 
|         for (MultipartFile file : files) {  | 
|             //是否格式正确  | 
|             boolean isGreate=true;  | 
|             if(file.getContentType().equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)){  | 
|                 String suffix = FileUtils.getSuffixByFile(file);  | 
|                 if(!suffix.equals("jpg")&&!suffix.equals("png")){  | 
|                     isGreate=false;  | 
|                 }  | 
|             }else {  | 
|                 if (!"image/png".equals(file.getContentType()) && !"image/jpeg".equals(file.getContentType()) && !"image/jpg".equals(file.getContentType())) {  | 
|                    isGreate=false;  | 
|                 }  | 
|             }  | 
|             if(!isGreate) {  | 
|                 result.setCode(-1);  | 
|                 result.setMsg("只支持JPG/PNG格式的图片");  | 
|                 log.debug("当前文件格式为:{}", file.getContentType());  | 
|                 return result;  | 
|             }  | 
|   | 
|             sumSize += file.getSize();  | 
|         }  | 
|   | 
|         //限制24m图片  | 
|         long maxSize = 24 * 1024 * 1024;  | 
|   | 
|         if (maxSize < sumSize) {  | 
|             result.setCode(-1);  | 
|             result.setMsg("图片总共不能超过24M");  | 
|             return result;  | 
|         }  | 
|   | 
|   | 
|         List<ResourceInfo> results = new ArrayList<>();  | 
|         for (MultipartFile file : files) {  | 
|             String suffix = StrUtil.subAfter(file.getOriginalFilename(), ".", true);  | 
|             String realName = IdUtil.fastSimpleUUID() + "." + suffix;  | 
|             FileUtil.writeFromStream(file.getInputStream(), Constants.IMG_UPLOAD_PATH + "/" + realName);  | 
|             ResourceInfo resourceInfo = new ResourceInfo();  | 
|             if(file.getContentType().equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)){  | 
|                 suffix = FileUtils.getSuffixByFile(file);  | 
|                 if(suffix.equals("jpg")){  | 
|                     resourceInfo.setResourceSubType("image/jpg");  | 
|                 }else if(suffix.equals("png")){  | 
|                     resourceInfo.setResourceSubType("image/png");  | 
|                 }  | 
|             }else {  | 
|                 resourceInfo.setResourceSubType(file.getContentType());  | 
|             }  | 
|             resourceInfo.setResourceOldName(file.getOriginalFilename());  | 
|             resourceInfo.setResourceSize(file.getSize());  | 
|             resourceInfo.setResourceUrl(businessUtil.getViewUrl() + "/" + realName);  | 
|             resourceInfo.setResourceType(Convert.toShort(type));  | 
|             resourceInfo.setCreateTime(DateUtil.now());  | 
|             resourceInfo.setCreateUserId(getUserId(request));  | 
|             resourceInfo.setRealName(realName);  | 
|             results.add(resourceInfo);  | 
|         }  | 
|         resourceInfoMapper.insertList(results);  | 
|         result.setData(results);  | 
|         return result;  | 
|     }  | 
|   | 
|   | 
|     @ApiOperation(value = "文件删除", notes = "id:返回的文件资源ID,多个用逗号分隔")  | 
|     @PostMapping(value = "/file/delete")  | 
|     public Result uploadDelete(@RequestBody CommonDto commonDto) {  | 
|         String[] split = commonDto.getId().split(",");  | 
|         for (String id : split) {  | 
|             ResourceInfo resourceInfo = resourceInfoMapper.selectByPrimaryKey(id);  | 
|             resourceInfoMapper.deleteByPrimaryKey(id);  | 
|             if (resourceInfo != null) {  | 
|                 String path = Constants.IMG_UPLOAD_PATH + "/" + resourceInfo.getRealName();  | 
|                 FileUtil.del(path);  | 
|             }  | 
|         }  | 
|         return Result.success("");  | 
|     }  | 
|   | 
|   | 
| }  |