1、控制器代码
private int imgWidth=150;//缩率图宽度
private int imgHeight=150;//缩率图宽度
//上传图片
@PostMapping("/imgOtherUpload")
public ResponseResult uploadOtherImg(@RequestParam("multipartFile") MultipartFile multipartFile,Integer checkUserId) {
try {
if (multipartFile.isEmpty() || StringUtils.isBlank( multipartFile.getOriginalFilename() )) {
return new ResponseResult( CommonCode.INVALID_PARAM );
}
String contentType = multipartFile.getContentType();
if (!contentType.contains( "image" )) {
return new ResponseResult( CommonCode.INVALID_PARAM );
}
String root_fileName = multipartFile.getOriginalFilename();
logger.info( "上传图片:name={},type={}", root_fileName, contentType );
String date = DateFormatUtils.format( new Date(), "/yyyy/MM/dd/" );
String directoryName = "other";
String filePath = location + "/"+directoryName + date;
logger.info( "图片保存路径={}", filePath );
String suff = root_fileName.substring( root_fileName.lastIndexOf( "." ) );
//原图
String uuid = UUID.randomUUID().toString();
String file_name_max = uuid+"_max" + suff;
try {
FileUtils.saveFile( multipartFile, filePath, file_name_max );
} catch (IOException e) {
e.printStackTrace();
}
//缩率图
String file_name_min = uuid+"_min" + suff;
//
FileUtils.thumbnail( multipartFile,filePath,file_name_min,imgWidth,imgHeight );
HashMap<String, String> map = new HashMap<>();
map.put( "img_path", "/"+directoryName+date + file_name_min );
map.put( "img_show", domain +"/"+directoryName+ date + file_name_min );
return new ResponseResult( CommonCode.SUCCESS, map );
} catch (Exception e) {
e.printStackTrace();
return new ResponseResult( CommonCode.FAIL );
}
}
2、缩率图方法
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
/**
* 保存缩率图
* @param multipartFile 图片文件
* @param path 保存的路径
* @param fileName 文件名
* @param sWidth 缩率图宽度
* @param sHeight 缩率图高度
*/
public static void thumbnail(MultipartFile multipartFile, String path, String fileName,int sWidth,int sHeight ) {
OutputStream oStream = null;
try {
String des = path + fileName;
oStream = new FileOutputStream( des );
//ImageIO获取图片流信息
Image image = ImageIO.read( multipartFile.getInputStream() );
int width = image.getWidth( null ); //获取原图宽度
int height = image.getHeight( null );//获取原图高度
int wrate = width / sWidth; //宽度缩略图
int hrate = height / sHeight;//高度缩略图
int rate = 0;
if (wrate > hrate) {//宽度缩略图比例大于高度缩略图,使用宽度缩略图
rate = wrate;
} else {
rate = hrate;
}
//计算缩略图最终的宽度和高度
int newWidth = width / rate;
int newHeight = height / rate;
BufferedImage bufferedImage = new BufferedImage( newWidth, newHeight, BufferedImage.TYPE_INT_RGB );
//图片缩略图实现
bufferedImage.getGraphics().drawImage( image.getScaledInstance( newWidth, newHeight, image.SCALE_SMOOTH ), 0, 0, null );
//*image/jpeg
String imageType = multipartFile.getContentType().substring( multipartFile.getContentType().indexOf( "/" ) + 1 );
ImageIO.write( bufferedImage, imageType, oStream );
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
oStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}