struts2中上传图片,怎么将图片直接保存到数据中?是否可以实现?

解决方案 »

  1.   

    我有将图片和缩略图放进数据库的例子,不过没有用到 struts..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 java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ToArrayPhoto {

    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");

    PreparedStatement pstm = conn.prepareStatement(
    "insert into FACELOOK_PHOTO values(FACELOOK_PHOTO_SEQ.nextval,sysdate,'照片',?,null,1,?,null)");
    pstm.setBytes(1, toByteArray());
    pstm.setBytes(2, buildThumbnail());

    System.out.println(pstm.executeUpdate());
    }

    /**
     * 将上传的图片保存成字符数组,在提交到数据库
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray() throws IOException{
    byte[] data = null;

    //创建文件流对象
    FileInputStream fileInputStream = new FileInputStream("F:\\资料站\\项目\\images\\1.jpg.jpg");
    //缓存对象
    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    //写入对象
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    //首先读取一次,如果返回-1则说明读取完毕
    int count = bufferedInputStream.read(); while(count!=-1){
    byteArrayOutputStream.write(count);
    count = bufferedInputStream.read();
    }
    byteArrayOutputStream.flush();
    byteArrayOutputStream.close();
    bufferedInputStream.close();
    fileInputStream.close();

    data = byteArrayOutputStream.toByteArray();
    return data;



    /**
     * 生成缩略图,保存成字符数组
     * @return
     * @throws IOException 
     */
    public static byte[] buildThumbnail() throws IOException{
    byte[] data = null;
    //构造一个image对象
    Image img = ImageIO.read(new File("F:\\资料站\\项目\\images\\1.jpg.jpg"));
    //得到原图的宽度和高度
    int oldWidth = img.getWidth(null);
    int oldHeight = img.getHeight(null);

    //初始化缩略图的宽度和高度
    int newWidth = 0;
    int newHeight = 0;
    int divWidth = 200;//限制宽度为200 

    if(oldWidth>divWidth){
    //做处理,否则不做
    int temp = oldWidth / divWidth ;
    newWidth = oldWidth / temp;
    newHeight = oldHeight / temp;
    }else{
    newHeight = oldHeight;
    newWidth = oldWidth;
    }
    // int imageWidth = 200;
    // int imageHeight = 100;

    BufferedImage bufferedImage = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics2D = bufferedImage.createGraphics();
    graphics2D.setBackground(Color.WHITE);
    graphics2D.clearRect(0, 0, newWidth, newHeight);
    //绘制缩小后的图
    bufferedImage.getGraphics().drawImage(img, 0, 0, newWidth, newHeight,null);

    //创建写入文件流对象
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    //缓存对象
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);

    JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(bufferedOutputStream);

    jpegImageEncoder.encode(bufferedImage);

    data = byteArrayOutputStream.toByteArray();
    bufferedOutputStream.close();
    byteArrayOutputStream.close();

    return data;
    }
    }