java上传图片处理

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

public class ImgUtil {

/**
* 生成图像(不强制按目标宽高生成图片)
*
* @param fileSrc
* 源文件
* @param pathDest
* 生成图片的路径
* @param widthDest
* 目标图片宽(如果为-1,表示可以任意宽度)
* @param heightDest
* 目标图片高(如果为-1,表示可以任意高度)
* @return
*/
public static boolean createImage(File fileSrc, String pathDest,
int widthDest, int heightDest) {
return createImage(false, fileSrc, pathDest, widthDest, heightDest);

}

/**
* 生成图像
*
* @param isForceWidthHeightToDest
* 是否按设定目标的宽高强制生成图片
* @param fileSrc
* 源文件
* @param pathDest
* 生成图片的路径
* @param widthDest
* 目标图片宽(如果为-1,表示按原图比率设定宽度)
* @param heightDest
* 目标图片高(如果为-1,表示按原图比率设定高度)
* @return
*/
public static boolean createImage(boolean isForceWidthHeightToDest,
File fileSrc, String pathDest, int widthDest, int heightDest) {
boolean flag = false;
File out = null;
try {
if (widthDest <= 0 && heightDest <= 0)// 如果目标宽高均<=0,则无法生成图片
return false;
else if (widthDest <= 0 || heightDest <= 0)
isForceWidthHeightToDest = false;
int w = 1;
int h = 1;
Image imgSrc = javax.imageio.ImageIO.read(fileSrc);// 源图片
if (isForceWidthHeightToDest) {// 如果强制,则将图片宽高设定为目标宽高
w = widthDest;
h = heightDest;
} else {
int width = imgSrc.getWidth(null);
int height = imgSrc.getHeight(null);
double imgRatio = width * 1.0 / height;
if (widthDest > 0 && heightDest > 0) {
w = widthDest;
h = (int) (widthDest / imgRatio);
if (h > heightDest) {
w = (int) (heightDest * imgRatio);
h = heightDest;
}
} else if (widthDest <= 0) {// 宽无限制
w = (int) (heightDest * imgRatio);
h = heightDest;
} else {// 高无限制
w = widthDest;
h = (int) (widthDest / imgRatio);
}
}
BufferedImage tag = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(imgSrc, 0, 0, w, h, null);
out = new File(pathDest);
flag = writeImageFile(out, tag, 80);
} catch (IOException ex) {
flag = false;
ex.printStackTrace();
} finally {
try {
if (out != null)
out = null;
} catch (Exception e1) {

}
}
return flag;
}

/**
* 将 BufferedImage 编码输出成硬盘上的图像文件
*
* @param file
* 编码输出的目标图像文件,文件名的后缀确定编码格式。
* @param image
* 待编码的图像对象
* @param quality
* 编码压缩的百分比
* @return 返回编码输出成功与否
*/
private static boolean writeImageFile(File fileDest,
BufferedImage imageSrc, int quality) {
try {
Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix("jpg");
if (it.hasNext()) {
FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(
fileDest);
ImageWriter iw = (ImageWriter) it.next();
ImageWriteParam iwp = iw.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality / 100.0f);
iw.setOutput(fileImageOutputStream);
iw.write(null,
new javax.imageio.IIOImage(imageSrc, null, null), iwp);
iw.dispose();
fileImageOutputStream.flush();
fileImageOutputStream.close();
}
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
}


// 上传文件域对象
private File upload;
// 上传文件名
private String uploadFileName;
// 上传文件类型
private String uploadContentType;
/**
* 上传图片处理
*
* @param name 图片名称(不可为空)
*
* @return 操作成功,返回true 操作失败,返回false
*/
public boolean uploadImg(String name){
boolean flag =false;
String dstPath = MyConstant.PATH + name;
@SuppressWarnings("unused")
File dstFile = new File(dstPath);

if (ImgUtil.createImage(upload, dstPath,
MyConstant.WIDTH,
MyConstant.HEIGHT)){
flag = true;
}
return flag;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 图片压缩处理可以使用 Java 图像处理库 ImageIO 和 Java 压缩库 ZipOutputStream。 以下是一个简单的 Java 图片压缩处理实现示例: 1. 获取上图片文件。 2. 使用 ImageIO 读取图片文件,将其转换为 BufferedImage 对象。 3. 使用 BufferedImage 的 getScaledInstance() 方法缩放图片。 4. 使用 ImageIO 将缩放后的 BufferedImage 对象写入临时文件。 5. 使用 ZipOutputStream 压缩临时文件。 6. 将压缩后的文件上到服务器。 以下是示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.imageio.ImageIO; public class ImageUploadCompress { public void uploadAndCompress(File imageFile, String zipFileName, int width, int height) throws IOException { // 读取图片文件 BufferedImage originalImage = ImageIO.read(new FileInputStream(imageFile)); // 缩放图片 BufferedImage scaledImage = originalImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); // 写入临时文件 File tempFile = File.createTempFile("temp_", ".jpg"); ImageIO.write(scaledImage, "jpg", tempFile); // 压缩临时文件 ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFileName)); zip.putNextEntry(new ZipEntry(tempFile.getName())); FileInputStream in = new FileInputStream(tempFile); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { zip.write(buffer, 0, len); } zip.closeEntry(); in.close(); zip.close(); // 将压缩后的文件上到服务器 // ... } } ``` 此示例仅演示了基本的图片压缩处理,实际应用中还需要考虑文件上安全和性能等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值