关闭

上传到数据库的类

601人阅读 评论(0) 收藏 举报

package com;
import com.jspsmart.upload.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.jsp.*;
import java.sql.PreparedStatement;
public class UploadDb {
  public UploadDb() {
  }
//上传函数
  public void upLoadDb(PageContext pageContext1) {
    com.jspsmart.upload.Request req=null;
    PreparedStatement pst=null;
    DBConnection db=new DBConnection();
    Connection con=db.getConnection();
    SmartUpload su = new SmartUpload();
    try
    {
      su.initialize(pageContext1);
      su.upload();
      req = su.getRequest();

      com.jspsmart.upload.Files files = su.getFiles();
      int len = files.getCount();
      for (int i = 0; i < len; i++) {
        com.jspsmart.upload.File fil = files.getFile(i);
        String filExt = null;
        String fileName = null;
        byte[] ndata = null;
        if (!fil.isMissing()) {
          filExt = fil.getFileExt(); //后缀
          fileName = fil.getFileName(); //名字
          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          for (int i2 = 0; i2 < fil.getSize(); i2++) {
            byte data = fil.getBinaryData(i2);
            bout.write( (int) data);
          }

          ndata = bout.toByteArray();
        }
        pst = con.prepareStatement(
            "insert into picdb(id,picname,picstream)values(?,?,?)");
        pst.setInt(1, 1);
         pst.setString(2, fileName);
        pst.setBytes(3, ndata);

        pst.execute();

      }
    }
    catch (Exception e1) {
      System.out.println("Exception:" + e1);
    }
  }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:93772次
    • 积分:1107
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:8篇
    • 译文:0篇
    • 评论:29条
    最新评论
    java基础知识