/**
* @param oldFile 要进行压缩的文件全路径
* @param width 压缩后的宽度
* @param height 压缩后的高度
* @param newFile 压缩后的文件的全路径
* @return 返回压缩后的文件的全路径
*/
public static String zipImageFile(String oldFile, int width, int height,
String newFile) {
if (oldFile == null) {
return null;
}
String newImage = null;
try {
/**对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(new File(oldFile));
/** 宽,高设定 */
int bImgType=BufferedImage.TYPE_INT_RGB;
String prefix=oldFile.substring(oldFile.lastIndexOf(".")+1);
if("png".equals(prefix)){
bImgType=BufferedImage.TYPE_INT_ARGB;
}
BufferedImage tag = new BufferedImage(width, height, bImgType);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
ImageIO.write(tag, prefix, new File(newFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
}
java 后台图片压缩上传png适用
最新推荐文章于 2024-04-29 09:16:42 发布