public static String uploadImg(MultipartFile file, int width, int height,
boolean whFixed, boolean borderRadius, boolean needCompress,
long maxSize) throws SvcSuspendedException {
PropertiesInjectUtil propertiesInjectUtil = SpringContextHolder
.getBean("propertiesInjectUtil");
String fileName = file.getOriginalFilename();
int suffixIndex = fileName.lastIndexOf('.');
if (suffixIndex == -1) {
throw new SvcSuspendedException("上傳失敗!文件沒有後綴名,不允許上傳!");
}
String name = fileName.substring(0, suffixIndex);
if (StringUtils.isBlank(name)) {
throw new SvcSuspendedException("上傳失敗!該文件名沒有文件名,不允許上傳!");
}
String suffix = file.getContentType();
if (!UploadUtil.acceptImg.contains(suffix)) {
throw new SvcSuspendedException("請上傳正確的圖片格式,目前僅支持jpg格式");
}
if (file.getSize() > maxSize * 1000) {
throw new SvcSuspendedException("上傳圖片不能大於" + maxSize + "K");
}
try {
BufferedImage srcBufferImage = ImageIO.read(file.getInputStream());
if (whFixed) {
if (srcBufferImage.getWidth() != width
|| srcBufferImage.getHeight() != height) {
throw new SvcSuspendedException("圖片長度必須爲" + width
+ "像素,高度必須爲" + height + "個像素");
}
}
InputStream is = null;
if (borderRadius) {
if (!BORDER_RADIUS_ACCEPT_IMG_TYPE.contains(suffix)) {
throw new SvcSuspendedException("僅PNG格式圖片支持圓角");
}
BufferedImage rounded = makeRoundedCorner(srcBufferImage, 30);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(rounded, "png", baos);
is = new ByteArrayInputStream(baos.toByteArray());
} else {
is = file.getInputStream();
}
String filePath = propertiesInjectUtil.getUpload_img_url();
String path = UploadUtil.upload(is, fileName, filePath,
RequestUtils.getWebpath() + "/upload/images");
if (needCompress) {
String dest = UploadUtil.generateLocalRandomPath(fileName,
filePath);
ImageUtil.compressImg(80, path, dest);
String url = UploadUtil.convertLocalPathToUrl(dest,
RequestUtils.getWebpath() + "/upload/images");
return url;
}
return path;
} catch (IOException e) {
log.error("Errors when read or update.", e);
throw new SvcSuspendedException("上傳圖片失敗!");
}
catch (MagickException e) {
log.error("Errors when compress images.", e);
throw new SvcSuspendedException("圖片壓縮失敗!");
}
}
makeRoundedCorner方法:
public static BufferedImage makeRoundedCorner(BufferedImage image,
int cornerRadius) {
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h,
BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) output.getGraphics();
// g2.setBackground(Color.WHITE);
// g2.clearRect(0, 0, w, h);
// This is what we want, but it only does hard-clipping, i.e. aliasing
// g2.setClip(new RoundRectangle2D ...)
// so instead fake soft-clipping by first drawing the desired clip shape
// in fully opaque white with antialiasing enabled...
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
Shape s = new RoundRectangle2D.Float(0, 0, w, h, cornerRadius,
cornerRadius);
g2.fill(s);
// ... then compositing the image on top,
// using the white shape from above as alpha source
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null);
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_DEFAULT);
g2.setBackground(Color.WHITE);
g2.setColor(Color.WHITE);
for (int x = 0; x <= w; x++) {
for (int y = 0; y <= h; y++) {
if (!s.contains(x, y)) {
if (x < w / 2) {
if (y < h / 2) {
g2.drawRect(x, y, 1, 1);
} else {
g2.drawRect(x, y - 1, 1, 1);
}
} else {
if (y < h / 2) {
g2.drawRect(x - 1, y, 1, 1);
} else {
g2.drawRect(x - 1, y - 1, 1, 1);
}
}
}
}
}
g2.dispose();
return output;
}