package com.bdqn.hr.util;
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 org.apache.commons.logging.Log;
import org.apache.commons.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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
struts2,spring和hibernate集成实现图片上传下载的一个小示例
共36个文件
xml:9个
java:7个
class:7个
4星 · 超过85%的资源 需积分: 9 101 下载量 126 浏览量
2013-06-09
15:25:32
上传
评论 1
收藏 28KB ZIP 举报
温馨提示
使用struts2,spring和hibernate集成实现图片上传下载的一个小示例,可以运行,例子中没有jar包,需要自行下载添加。
资源推荐
资源详情
资源评论
收起资源包目录
photo.zip (36个子文件)
photo
.mystrutsdata 240B
seql.sql 255B
.myhibernatedata 504B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
src
com
bdqn
hr
dao
TestPhotoDao.java 238B
impl
TestPhotoDaoImpl.java 820B
action
UpdateOrSaveAction.java 3KB
biz
TestPhotoBiz.java 243B
impl
TestPhotoBizImpl.java 832B
entity
TestPhoto.hbm.xml 1KB
TestPhoto.java 884B
util
FileUtil.java 3KB
struts.xml 699B
hibernate.cfg.xml 909B
applicationContext.xml 2KB
.project 2KB
WebRoot
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 1KB
classes
com
bdqn
hr
dao
TestPhotoDao.class 358B
impl
TestPhotoDaoImpl.class 1KB
action
UpdateOrSaveAction.class 4KB
biz
TestPhotoBiz.class 358B
impl
TestPhotoBizImpl.class 1KB
entity
TestPhoto.hbm.xml 1KB
TestPhoto.class 1KB
util
FileUtil.class 4KB
struts.xml 699B
hibernate.cfg.xml 909B
applicationContext.xml 2KB
lib
index.jsp 1KB
show.jsp 1KB
.mymetadata 288B
.classpath 3KB
.myeclipse
seql.~sql 255B
共 36 条
- 1
资源评论
- weixin_381957702017-10-24不知怎么下载是崩了
- qq_344795312017-11-07没用,试试看
- FengQingSu2015-05-07没用上传成功,不知道是不是自己没配好环境,还是什么别的原因
- baidu_288437152015-06-09下载来研究
huaishuiyipen
- 粉丝: 2
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功