up:: Excel批量添加商品
说明: 对上传的图片进行处理,比如添加水印,图片缩放,防止友商盗图
正式开发
1. 引入依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
学习一个新的组件最好是查看他的官网 Examples · coobird/thumbnailator Wiki · GitHub
2.新建ImageUtil工具类,进行演示!!!
package com.imooc.mall.util;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
/**
* 描述: 图片工具类
*/
public class ImageUtil {
public static void main(String[] args) throws IOException {
String path = "/Users/apple/Desktop/";
//切割
Thumbnails.of(path + "caomei.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 200, 200).size(200, 200).toFile(path+"crop.jpg");
//缩放
Thumbnails.of(path+"caomei.jpg").scale(0.7).toFile(path+"scale1.jpg");
Thumbnails.of(path+"caomei.jpg").scale(1.5).toFile(path+"scale2.jpg");
Thumbnails.of(path+"caomei.jpg").size(500,500).keepAspectRatio(false).toFile(path+"size1.jpg");
Thumbnails.of(path+"caomei.jpg").size(500,500).keepAspectRatio(true).toFile(path+"size2.jpg");
//旋转
Thumbnails.of(path+"caomei.jpg").size(500,500).keepAspectRatio(true).rotate(90).toFile(path+"rotate1.jpg");
Thumbnails.of(path+"caomei.jpg").size(500,500).keepAspectRatio(true).rotate(180).toFile(path+"rotate2.jpg");
}
}
说明:
3.开发controller层
/**
* 上传文件(这儿具体来说,就是图片)
* @param httpServletRequest
* @param file
* @return
*/
@ApiOperation("上传文件(这儿具体来说,就是图片)")
@PostMapping("/admin/upload/image")
@ResponseBody
public ApiRestResponse uploadImage(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) {
//获取文件的原始名字
String fileName = file.getOriginalFilename();
//通过截取最后一个“.”后面的内容,获取文件扩展名
String suffix = fileName.substring(fileName.lastIndexOf("."));
//利用UUID,生成文件上传到服务器中的文件名;
UUID uuid = UUID.randomUUID();//通过Java提供的UUID工具类,获取一个UUID;
//把uuid和文件扩展名,拼凑成新的文件名;
String newFileName = uuid.toString() + suffix;
//生成文件夹的File对象;
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
//生成文件的File对象;
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
//如果文件夹不存在的话
createFile(file, fileDirectory, destFile);
try {
Thumbnails.of(destFile).size(Constant.IMAGE_SIZE,Constant.IMAGE_SIZE).watermark(Positions
.BOTTOM_RIGHT, ImageIO.read(new File(Constant.FILE_UPLOAD_DIR + Constant.WATER_MARK_JPG)),
Constant.IMAGE_OPACITY).toFile(new File(Constant.FILE_UPLOAD_DIR + newFileName));
} catch (IOException e) {
e.printStackTrace();
}
//执行到这儿以后,表示,我们已经把文件,存放到指定的位置了;
//接下来,就是组织图片的url,返回给前端;
try {
// System.out.println(httpServletRequest.getRequestURL() + "");
// System.out.println(getHost(new URI(httpServletRequest.getRequestURL() + ""))); return ApiRestResponse.success(
getHost(new URI(httpServletRequest.getRequestURL() + "")) +
"/images/" + newFileName);
} catch (URISyntaxException e) {
//如果上面的过程出现了问题,就抛出文件上传失败异常;
return ApiRestResponse.error(ImoocMallExceptionEnum.UPLOAD_FAILED);
}
}
private void createFile(@RequestParam("file") MultipartFile file, File fileDirectory, File destFile) {
if (!fileDirectory.exists()) {
//如果在创建这个文件夹时,创建失败,就抛出文件夹创建失败异常
if (!fileDirectory.mkdir()) {
throw new ImoocMallException(ImoocMallExceptionEnum.MKDIR_FAILED);
}
}
//如果能执行到这儿,说明文件夹已经创建成功了;;;那么就把传过来的文件,写入到我们指定的File对象指定的位置中去;
try {
file.transferTo(destFile);
} catch (IOException e) {
e.printStackTrace();
}
}
这是图片上传接口,忘记的请复习SpringBoot电商项目商品模块增加商品接口之图片上传、SpringBoot电商项目商品模块增加商品接口之上传图片接口开发
此处方法类似与我们前面的上传EXCEL文件进行解析,只不过此处变成了上传图片,复制过来进行修改即可
为了防止代码复用,我们将一部分代码抽取出来:
说明:
水印来源: