我自己改写了这个类,先存成文件,然后再写入数据库。可是,我还是想知道,我的错误在哪儿???哪位帮我回答。谢谢!

解决方案 »

  1.   

    这是我昨天修改后的,先存入硬盘,然后再写入数据库的。
    package upload;import gcs.db.*;
    import com.jspsmart.upload.*;
    import javax.servlet.jsp.*;
    import java.sql.*;
    import java.io.*;public class UploadImage extends GJdbc {
        // assign to database columns
        public int intID = 0;
        public InputStream istPhoto;
        public String strName = "";
        public String strContentType = "";
        public int intLength = 0;    // jspSmartUpload objects
        protected SmartUpload m_SmartUpload;
        protected com.jspsmart.upload.Request m_Request;
        protected com.jspsmart.upload.Files m_Files;
        protected com.jspsmart.upload.File m_File;
        protected String m_strFileName;
        public UploadImage () {
            Connect ("localhost", "test", "sa", "123456");
        }    public void upload(PageContext p) {
            m_SmartUpload = new SmartUpload();        m_strFileName = p.getServletContext().getRealPath("./jspSmartUpload.tmp");        try {
                m_SmartUpload.initialize (p);
                m_SmartUpload.setMaxFileSize (1024 * 1024);
                m_SmartUpload.setAllowedFilesList ("jpg,jpeg,gif,bmp,swf");
                m_SmartUpload.upload ();            m_Request = m_SmartUpload.getRequest ();
                m_Files = m_SmartUpload.getFiles ();
            }
            catch (Exception e) {
                e.printStackTrace ();
            } // try ... catch
        }    public int SaveToDB() {
            java.io.File fileTemp;
            java.io.InputStream inStream;
            int intFileSize;        String strSql;
            String strName;
            int intNewID = -1;        try {
                // 将文件写入硬盘
                m_File = m_Files.getFile (0);
                m_File.saveAs (m_strFileName);
                intFileSize = m_File.getSize();            // 将文件从硬盘读出, 存成二进制流
                fileTemp = new java.io.File(m_strFileName);
                inStream = new java.io.FileInputStream(fileTemp);            strName = m_Request.getParameter ("txtName");            // 添加其它信息
                strSql = "INSERT INTO jsp_upload (name, photo, contenttype, length) VALUES (?, ?, ?, ?)";
                strSql += " SELECT @@IDENTITY AS 'new_id'";
                m_PrepStatement = m_Connection.prepareStatement (strSql);
                m_PrepStatement.setString (1, strName);
                m_PrepStatement.setBinaryStream (2, inStream, intFileSize);
                m_PrepStatement.setString (3, m_File.getContentType ());
                m_PrepStatement.setInt (4, intFileSize);
                m_ResultSet = m_PrepStatement.executeQuery ();            if (m_ResultSet.next()) {
                    intNewID = m_ResultSet.getInt ("new_id");
                } // if            inStream.close();
            }
            catch (Exception e) {
                e.printStackTrace ();
            } // try ... catch        return intNewID;
        }    public void Browse() {
            executeQuery ("SELECT * FROM jsp_upload ORDER BY id");
            MoveFirst ();
        }    public void view(int id) {
            executeQuery ("SELECT * FROM jsp_upload WHERE id=" + id);
            MoveFirst ();
        }    public void toHTML () {
            try {
                intID = m_ResultSet.getInt ("id");
                strName = m_ResultSet.getString ("name");
                strContentType = m_ResultSet.getString ("contenttype");
                istPhoto = m_ResultSet.getBinaryStream ("photo");
                intLength = m_ResultSet.getInt ("length");
            }
            catch (Exception e) {
                e.printStackTrace ();
            } // try ... catch
        }
    }