我已经能在action中获得上传的文件了,下一步要求把数据放入数据库表中的blob字段里,这个blob的实现类在哪里。。

解决方案 »

  1.   

    你需要使用某些上传jar包来帮你
      

  2.   

    一般这样的类是不用自己去写的。。在网上找找 有这样的jar包。。比如struts中就有自带的或者smartUpload也可以。。
      

  3.   

    我这是个小项目,无论用户上传什么类型的文件(都很小),我都把它放进数据库表中的blob字段里。我用的ssh,持久化类中的对应属性就是Blob类型的,java.sql.blob不是一个接口么,我找不到Blob的实现类。。不会持久化。。请大家指点一下啊,我在一个书上看到说用Hibernate.createBlob()这个,不知这是不是正常套路?望各位大侠指点
      

  4.   

    哦,可我不是用这个方法,我有代码的我给你util类吧,你自己看下package com.shopzdw.shop.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 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;

    }