紧急求助,那位大侠能帮我解决一下上传图片到数据库的问题,谢谢。(200分)

  • 主题发起人 主题发起人 qiqijsp
  • 开始时间 开始时间
Q

qiqijsp

Unregistered / Unconfirmed
GUEST, unregistred user!
我用jsp+sqlserver2000,上传图片用SmartUpload的Bean(只是先传到服务器一个目录),
可是在传入数据库时出了问题,共有四个字段(id(自动加一),name,password,picture)
,以下是源文件,出现空点错误[8D]
<html>
<head>
<title>存储图片</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%@ page language="java" contentType="text/html;charset=gb2312" %>
<%@ page import="java.io.*,java.sql.*,sql.*,com.jspsmart.upload.SmartUpload" %>
<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String aaaa="0";
int count=0;
myUpload.initialize(pageContext);
//这里使用了SmartUpload的Bean;
myUpload.setTotalMaxFileSize(512000);
myUpload.upload();
String names=request.getParameter("name");
//这里进行了中文转换;
byte[] temp_names=names.getBytes("ISO8859_1");
names=new String(temp_names);
String password=request.getParameter("password");
byte[] temppassword=password.getBytes("ISO8859_1");
password=new String(temppassword);
//连接数据库;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:conn";
Connection conn=DriverManager.getConnection(url,"sa","sa");
Statement stmt=conn.createStatement();
String sql="insert into testid (name,password) values('"+names+"','"+password+"')";
stmt.executeUpdate(sql);
String selectsql="select @@identity as 'maxnum' from testid";//选出数据库中id的最大值,以便向这个记录插入图片;
ResultSet rs=stmt.executeQuery(selectsql);
if(rs.next()){
aaaa=rs.getString("maxnum");
//返回最大值;
}
try{
int num=Integer.parseInt(aaaa);
count=myUpload.save("/upload");
File file=new File("/upload/"+myUpload.getFiles().getFile(0).getFileName());
InputStream in =new FileInputStream(file);
int filelength=(int)file.length();
PreparedStatement stm=conn.prepareStatement("update testid set picture=? where id="+num);
stm.setBinaryStream(1,in,filelength);
stm.executeUpdate();
}
catch(Exception e){}
out.print("success");
%>
</body>
</html>
那个SmartUpload组件有向数据库传图片的方法,可是向sqlserver2000好像不好使,我只有
使用上面的方法。如果那位大侠有更好的方法,希望你们能告诉我,谢谢。
 
你应该先把上传和写入DB这两步分开,看看到底哪步出了问题。
你用的是tomcat+apache么?
 
自己开发一个把图片写入数据库的JAVABEAN,这样容易测试
 
你到底么把图片放在那里?数据库?目录!
而且他是转换成流来传送的,SmartUpload有5个详细的例子!什么东西都在里面!
form中记得么ENCTYPE="multipart/form-data"转换成流!而且就手也不是直接REQUEST,要从流里面提出来!
mode:
<%@page
language="java"
import="java.sql.*"
contentType="text/html;charset=gb2312"%>
<jsp:useBean id="data" scope="page" class="mode.CommonDB"/>
<%
String entry_id=request.getParameter("id");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>用户添加</TITLE>
<META content="text/html;
charset=gb2312" http-equiv=Content-Type>
<SCRIPT language="javascript">
function check(){
if(document.frmUserInfo.title.value==""){
alert("请填写标题!");
do
cument.frmUserInfo.title.focus();
return false;
}

if(document.frmUserInfo.note.value==""){
alert("请填写内容!");
do
cument.frmUserInfo.note.focus();
return false;
}

return true;
}
</script>
<link rel="stylesheet" type="text/css" href="main.css">
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>

<table border=1 cellpadding=4 cellspacing=0 width="90%" bgcolor=#FFFFFF bordercolordark="#FFFFFF" bordercolorlight="#999999" align="center">
<form method=post action="newsinsert.jsp" name="frmUserInfo" onsubmit="return check();" ENCTYPE="multipart/form-data">
<tr bgcolor="#7dbdbd">
<td colspan="3" height="25">
<div align="center">新闻发布中心</div>
</td>
</tr>
<%
String sql;
ResultSet rs;
sql="select * from seentry where entry_id='"+entry_id+"'";
rs=data.executeQuery(sql);
%>
<tr>
<td width="25%">二级栏目</td>
<td colspan="2" width="75%">
<select name="lanmu">
<%
String lanmu;
while(rs.next()) {
lanmu = new String(rs.getString("seentry").getBytes(),"GB2312");
%>
<option value="<%=rs.getInt("id")%>"><%=lanmu%></option>
<%}%>
</select>
</td>
</tr>
<tr>
<td width="25%">标题</td>
<td colspan="2" width="75%">
<input type="text" name="title" size=60 maxsize=100>
</td>
</tr>
<tr>
<td width="25%">内容</td>
<td colspan="2" width="75%">
<textarea name="note" cols="70" rows="20"></textarea>
</td>
</tr>
<tr>
<td width="25%">图片</td>
<td colspan="2" width="75%">
<input name="pic" type="file" size=20 maxlength=30 value="浏览...">
</td>
</tr>
<tr >
<td width="25%">&amp;nbsp;</td>
<td colspan="2" width="75%">
<FONT color=#ff0033><B></B></FONT>
<P align=center>
<INPUT name=Submit type=submit value="添 加">
<INPUT name=Submit2 type=reset value="重 填">
</P>
</td>
</tr>
</FORM>
</table>

</BODY></HTML>

mode2:
<%@page
language="java"
import="java.sql.*,
com.jspsmart.upload.*,
java.util.*"
contentType="text/html;charset=gb2312"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<jsp:useBean id="data" scope="page" class="mode.CommonDB"/>
<jsp:useBean id="Calendar" scope="page" class="mode.PspCalendar"/>
<%
String picname="";
String sql;
ResultSet rs;
%>
<% //得到本条记录的picID
sql="select max(id) as picid from detail";
rs=data.executeQuery(sql);
int picid=0;
if(rs.next()){
picid=rs.getInt("picid")+1;
}
%>

<%
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (!myFile.isMissing()) {
myFile.saveAs("/pic/" +picid+"."+myFile.getFileExt());
picname=picid+"."+myFile.getFileExt();
}
%>
<%java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames();
String key = (String)e.nextElement();
String seentry_id = mySmartUpload.getRequest().getParameter("lanmu");
String title = mySmartUpload.getRequest().getParameter("title");
String note = mySmartUpload.getRequest().getParameter("note");
note = data.htmlReplace(note);
%>

<%
String mydate=Calendar.getDate();
String mytime=Calendar.getTime();
String mydatetime=mydate+" "+mytime;

sql="insert into detail(id,seentry_id,title,pic,regdate,note) values(detailid.nextval,'"+seentry_id+"','"+title+"','"+picname+"','"+mydatetime+"','"+note+"')";

if(data.executeUpdate(sql)){
%>
<br>
添加成功!
<%
}else
{
%>
<h1 align="center"><font color="red">出现错误,请重新再试一次!</font></h1>
<%
}
%>

自己试一下!
 
后退
顶部