【请教】这是我写的一段代码,使用jspSmartUpload上传内容,有些小问题。 我自己改写了这个类,先存成文件,然后再写入数据库。可是,我还是想知道,我的错误在哪儿???哪位帮我回答。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我昨天修改后的,先存入硬盘,然后再写入数据库的。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 }} Struts配置问题 策略模式在Struts+Hibernate框架中应该属于哪一层? 如何对下拉列表的列表进行逐行编辑? mysql用什么字段来存储文字和图片? java 实现二级域名 大侠们, 在网页中以 &#x 开头是UTF8编码吗,为什么我的程序把&#x后面的数值读到内存中用ICONV去转换到GB2312时,却得不到正确的字符 为什么连不上Oracle? 急救啊,跳转地址栏的问题 在数据库中添加记录 JSP/JAVA操作Excel的问题!在线等待!!! 怎样才能其它网站上的网页进行操作? 如何避免SQL语句中的单引号内出现单引号?
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
}
}