| | |
| | | package com.xcong.farmer.cms.modules.system.controller; |
| | | |
| | | import cn.hutool.core.img.Img; |
| | | import cn.hutool.core.img.ImgUtil; |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.xcong.farmer.cms.common.contants.AppContants; |
| | | import com.xcong.farmer.cms.common.response.Result; |
| | |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.coobird.thumbnailator.Thumbnails; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.imageio.ImageIO; |
| | | import javax.validation.Valid; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | |
| | | import java.io.InputStream; |
| | | import java.util.Map; |
| | | |
| | | |
| | |
| | | @Value("${static.resource.url}") |
| | | private String resourceUrl; |
| | | |
| | | @Value("${static.resource.path}") |
| | | private String resourcePath; |
| | | |
| | | @ApiOperation(value = "文件上传", notes = "文件上传") |
| | | @PostMapping("/uploadFile") |
| | | public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception { |
| | | |
| | | // 文件保存目录路径 |
| | | String savePath = AppContants.PICTURE_PATH; |
| | | String savePath = resourcePath; |
| | | // 文件保存目录URL |
| | | String saveUrl = resourceUrl; |
| | | // 检查目录 |
| | |
| | | if (!uploadDir.isDirectory()) { |
| | | uploadDir.mkdir(); |
| | | } |
| | | //获得文件的后缀 |
| | | |
| | | |
| | | // 获得文件的后缀 |
| | | String filename = IdUtil.simpleUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); |
| | | File filepath = new File(savePath + filename); |
| | | try { |
| | | //存文件 |
| | | file.transferTo(filepath); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | if (isImage(file.getInputStream())) { |
| | | Thumbnails.of(file.getInputStream()) |
| | | // 图片大小(长宽)压缩比例 从0-1,1表示原图 |
| | | .scale(1f) |
| | | // 图片质量压缩比例 从0-1,越接近1质量越好 |
| | | .outputQuality(0.5f) |
| | | .toOutputStream(new FileOutputStream(savePath + filename)); |
| | | } else { |
| | | File filepath = new File(savePath + filename); |
| | | try { |
| | | //存文件 |
| | | file.transferTo(filepath); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | String visitPath = (saveUrl + filename); |
| | | return Result.ok("上传成功",visitPath); |
| | | } |
| | | |
| | | |
| | | |
| | | private boolean isImage(InputStream inputStream) { |
| | | BufferedImage read = null; |
| | | try { |
| | | read = ImageIO.read(inputStream); |
| | | } catch (IOException e) { |
| | | return false; |
| | | } |
| | | return read != null; |
| | | } |
| | | } |