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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章