对上传的图片进行处理;
处理大量图片
/** * 1.判断文件是否为图片 jpg|png|gif * 2.防止恶意程序 高度和宽度判断 * 3.图片分文件保存 分布式fast|yyyy/MM/dd * 4.修改文件名称,防止重名 UUID */
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>实现文件长传</h1> <!--enctype="开启多媒体标签" --> <form action="http://localhost:80/file" method="post" enctype="multipart/form-data"> <input name="fileImage" type="file" /> <input type="submit" value="提交"/> </form> </body> </html> package com.jt.controller; import java.io.File; import java.io.IOException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.jt.service.FileService; import com.jt.vo.EasyUIFile; @RestController public class FileController { @Autowired private FileService fileService; /* * 1.准备文件存储目录 * 2.获取文件名称 * 3.利用工具API实现文件上传 */ @RequestMapping("/file") public String file(MultipartFile fileImage) throws IllegalStateException, IOException { File fileDir = new File("E:/java/img"); if(!fileDir.exists()) { //如果没有目录应该创建目录 fileDir.mkdirs(); } //获取图片名称 String imgName = fileImage.getOriginalFilename(); String path = "E:/java/img/"+imgName; //文件实现上传 fileImage.transferTo(new File(path)); return "文件上传成功!"; } /** * 实现商品的文件上传 */ @RequestMapping("/pic/upload") public EasyUIFile fileUpload(MultipartFile uploadFile) { return fileService.fileUpload(uploadFile); } } package com.jt.service; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; import javax.imageio.ImageIO; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.jt.vo.EasyUIFile; @Service public class FileServiceImpl implements FileService{ private String localDir = "E:/java/img/"; /** * 1.判断文件是否为图片 jpg|png|gif * 2.防止恶意程序 高度和宽度判断 * 3.图片分文件保存 分布式fast|yyyy/MM/dd * 4.修改文件名称,防止重名 UUID */ @Override public EasyUIFile fileUpload(MultipartFile uploadFile) { EasyUIFile easyUIFile = new EasyUIFile(); //1.判断文件是否为图片类型 String fileName = uploadFile.getOriginalFilename(); fileName = fileName.toLowerCase(); //解决文件名后缀是大写的 if(!fileName.matches("^.+\\.(jpg|png|gif)$")) { //表示不满足规则 return EasyUIFile.fail(); } //2.判断是否为恶意程序,转化成为图片对象 try { BufferedImage bufferedImage = ImageIO.read(uploadFile.getInputStream()); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); if(width==0||height==0) { return EasyUIFile.fail(); } //3.实现份文件存储 按照yyyy/MM/dd String dateDir = new SimpleDateFormat("yyyy/MM/dd/") .format(new Date()); //E:/java/img/yyyy/MM/dd String fileDirPath = localDir +dateDir; File dirFile = new File(fileDirPath); if(!dirFile.exists()) { dirFile.mkdirs(); } //4.生成文件防止重名 name.type int index = fileName.lastIndexOf("."); String fileType = fileName.substring(index); String uuid = UUID.randomUUID().toString(); //拼接文件名称 String realFileName = uuid+fileType; //5.文件实现上传 uploadFile.transferTo(new File(fileDirPath+realFileName)); //暂时使用网络地址代替真正的url地址 easyUIFile.setHeight(height) .setWidth(width) .setUrl("https://img11.360buyimg.com/n5/s54x54_jfs/" + "t1/63250/39/14040/164985/5db7ff7cE758e2798/91c7367ccef07bc2.jpg"); } catch (IOException e) { e.printStackTrace(); return EasyUIFile.fail(); } return easyUIFile; } }