1、控制器代码:
//上传图片
@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.cutImage( multipartFile,filePath,file_name_min,0,0,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、截取代码:
/**
* 指定坐标,截取正方形的图片
*
* @param multipartFile 图片
* @param path 路径
* @param fileName 文件名
* @param x 起始x位置
* @param y 起始y位置
* @param newWidth 要截取的宽度
* @param newHeight 要截取的高度
* @throws IOException
*/
public static void cutImage(MultipartFile multipartFile, String path, String fileName, int x,
int y, int newWidth, int newHeight) throws IOException {
ImageInputStream imageStream = null;
OutputStream oStream = null;
try {
String imageType = multipartFile.getContentType().substring( multipartFile.getContentType().indexOf( "/" ) + 1 );
System.out.println(imageType);
//保存路径
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 );//获取原图高度
if (width < newWidth) {
newWidth = width;
newHeight = width;
}
if (height < newWidth) {
newWidth = width;
newHeight = width;
}
//从原始图的中间截取方图片
if (width > newWidth) {
x = (width - newWidth) / 2;
}
if (height > newWidth) {
y = (height - newWidth) / 2;
}
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName( imageType );
ImageReader reader = readers.next();
imageStream = ImageIO.createImageInputStream( multipartFile.getInputStream() );
reader.setInput( imageStream, true );
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle( x, y, newWidth, newHeight );
param.setSourceRegion( rect );
BufferedImage bi = reader.read( 0, param );
ImageIO.write( bi, imageType, oStream );
} finally {
imageStream.close();
oStream.close();
}
}