我已经能在action中获得上传的文件了,下一步要求把数据放入数据库表中的blob字段里,这个blob的实现类在哪里。。
解决方案 »
- 十万火急!POI不能准确读取excel批注 求高人帮助!
- clipse 编译struts2 报错
- java ie 插件
- 帮忙下怎么把验证码传到后台做判断
- AppFuse搭建环境出现的问题,急.....
- jtds为何调用失败
- import com.microsoft.*出现错误了
- 高分求教:关于JTextField和JTable控件的两个问题。
- redhat 9.0下装过weblogic 7.0.
- "Untitled1.ejbgrpx": Spaces in the classpath may cause WebLogic EJBC utility to produce fatal compile errors.???
- 求一条HQL语句```
- 项目里DAO的定义
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 picture) throws IOException {
//获得文件对象的输入流
FileInputStream fis = new FileInputStream(picture);
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;
} /**
* 构建小样图片
* @param srcFile
* @return
*/
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(newWidth,
newHeight, 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;
}
}