图片上传的工具类:生成缩略图,文件流转换。
可与struts2集成使用
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import mons.logging.Log;
import mons.logging.LogFactory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class FileUtil {
private static Log log = LogFactory.getLog(FileUtil.class);
public static byte[] toByteArray(File photo) throws IOException
{
FileInputStream fis = new FileInputStream(photo);
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = bis.read();
while (c != -1) {
baos.write(c);
c = bis.read();
}
bis.close();
byte[] rtn = baos.toByteArray();
baos.close();
return rtn;
}
public static byte[] buildThumbnail(File srcFile) {
byte[] rtn = null;
try {
Image src = ImageIO.read(srcFile); // 构造Image对象
int oldWidth = src.getWidth(null); // 得到源图宽
int oldHeight = src.getHeight(null);// 得到源图高
log.debug("old width is " + oldWidth);
log.debug("old height is " + oldHeight);
float divWidth = 200f; // 限制宽度为200
int newWidth = 200; // 缩略图宽,
int newHeight = 0; // 缩略图高
float tmp;
if (oldWidth > newWidth) {
tmp = oldWidth / divWidth;
newWidth = Math.round(oldWidth / tmp);// 计算缩略图高
newHeight = Math.round(oldHeight / tmp);// 计算缩略图高
log.debug("tmp scale is " + tmp);
} else {
newWidth = oldWidth;
newHeight = oldHeight;
}
int imageHeight = 100;
int imageWidth = 200;
log.debug("new width is " + newWidth);
log.debug("new height is " + newHeight);
BufferedImage bufferedImage = new BufferedImage(imageWidth,
imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = (Graphics2D)
bufferedImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.clearRect(0, 0, imageWidth, imageHeight);
bufferedImage.getGraphics().drawImage(src,
//(imageWidth - oldWidth) / 2, (imageHeight - newHeight) /
2,
0,0,
newWidth, newHeight, null); // 绘制缩小后的图
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(bufferedImage); // 近JPEG编码
rtn = baos.toByteArray();
bos.close();
baos.close();
} catch (Exception e) {
log.debug(e);
} finally {
}
return rtn;
}
}