将客户端的图片上传到服务器的sql2000数据库时出错! (50)(50分)

  • 主题发起人 主题发起人 laozheng
  • 开始时间 开始时间
L

laozheng

Unregistered / Unconfirmed
GUEST, unregistred user!
环境:jsdk1.4.2+tomcat5+sql2000
原程序如下:
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BlobUploadServlet extends HttpServlet{

private static final char CR = 13;
private static final char LF = 10;

protected String boundary = null;
protected Hashtable params = new Hashtable();//

public voiddo
Post( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
ServletOutputStream out = response.getOutputStream();
ServletInputStream in = request.getInputStream();
BufferedInputStream bin = new BufferedInputStream(in);
boundary = getBoundary(request.getHeader("content-type"));

out.println("<html><body><pre>");
out.println("boundary =/n"+boundary);
out.println();

byte[] bytes = new byte[128];
in.readLine(bytes,0,bytes.length);
String line = new String(bytes);
Hashtable header = null;
while(in.readLine(bytes,0,bytes.length)>=0){
line = new String(bytes);
if(line.startsWith("Content-Disposition:")){
out.println(line);
header = parseHeader(line);
updateParams(header);
}else
if(line.startsWith("Content-Type:")){
params.put("Content-Type",
line.substring("Content-Type:".length()).trim());
}else
{
if(header!=null&amp;&amp;bytes[0]==13){
if(header.containsKey("filename")){
displayParams(out);
out.println(" ...saving payload");
savePayload(params,bin);
header = null;
}else
{
String name = (String)header.get("name");
String value = getParameter(in).trim();
params.put(name,value);
}
}if(line.indexOf(boundary)>=0)out.println(line);
}
bytes = new byte[128];
}
out.println("</pre></body></html>");
out.close();
}
private void displayParams(ServletOutputStream out)throws java.io.IOException{
for (Enumeration e = params.keys();e.hasMoreElements();) {
String key = (String)e.nextElement();
out.println(" "+key+" = "+params.get(key));
}
}
private void updateParams(Hashtable header){
for (Enumeration e = header.keys();e.hasMoreElements();) {
String key = (String)e.nextElement();
params.put(key,header.get(key));
}
}
private String getParameter(ServletInputStream in)throws java.io.IOException{
byte[] bytes = new byte[128];
in.readLine(bytes,0,bytes.length);
return new String(bytes);
}
private String getBoundary(String contentType){
int bStart = contentType.indexOf("boundary=")+"boundary=".length();
return "" + CR + LF + "--" + contentType.substring(bStart);
}
private void savePayload(Hashtable params,BufferedInputStream is)
throws java.io.IOException{
int c;
PushbackInputStream input = new PushbackInputStream(is,128);
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ( (c=read(input,boundary)) >= 0 )out.write( c );
int id = Integer.parseInt((String)params.get("ID"));
saveBlob(id,(String)params.get("filename"),out.toByteArray());
out.close();
}

private int read( PushbackInputStream input, String boundary )
throws IOException
{
StringBuffer buffer = new StringBuffer();
int index = -1;
int c;

do
{
c = input.read();

buffer.append( (char)c );
index++;
}while ( (buffer.length() < boundary.length()) &amp;&amp;
(c == boundary.charAt(index)) );

if ( c == boundary.charAt(index) ){
int type = -1;
if ( input.read() == '-' )
type = -2;
while ( input.read() != LF );
return type;
}
while ( index >= 0 ){
input.unread( buffer.charAt(index));
index--;
}
return input.read();
}
private Hashtable parseHeader(String line){
Hashtable header = new Hashtable();
String token = null;
StringTokenizer st = new StringTokenizer(line,";");
while(st.hasMoreTokens()){
token = ((String)st.nextToken()).trim();
String key = "";
String val = "";
int eq = token.indexOf("=");
if(eq <0) eq = token.indexOf(":");
if(eq >0){
key = token.substring(0,eq).trim();
val = token.substring(eq+1);
val = val.replace('"',' ');
val = val.trim();
header.put(key,val);
}
}
return header;
}
public void saveBlob(int id,String description,byte[] out){
String cmd = "INSERT INTO Photos (picid,infoid,picture) VALUES(?,?,?)";
System.out.println(cmd);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mxgbnet","sa","");
PreparedStatement pstmt=con.prepareStatement(cmd);
pstmt.setInt(1, id);

pstmt.setString(2, description);

pstmt.setBytes(3, out);
System.out.println(pstmt.executeUpdate());

con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
可以编译通过,但在jsp页面调用时出错,
jsp页面如下
<body>
<form action="BlobUploadServlet" enctype="multipart/form-data" //这儿有点疑问! method="post">
<input type="hidden" name="id" value="1">
<table border="1" >
<tr>
<td width="467" align="center">
<p>文件名:
<input type="file" name="submit-file" size="40">
</p>
<p>
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置">
</p></td>
</tr>

</table>
</form>
</body>
</html
出错信息如下:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:436)
java.lang.Integer.parseInt(Integer.java:518)
BlobUploadServlet.savePayload(BlobUploadServlet.java:87)
BlobUploadServlet.doPost(BlobUploadServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

note The full stack trace of the root cause is available in the Tomcat logs.

--------------------------------------------------------------------------------
Apache Tomcat/5.0
如果我把jsp页面 里form 的 enctype="multipart/form-data" 改为enctype="application/x-www-form-urlencoded"则结果
显示 boundary =
--ion/x-www-form-urlencoded
百思不得其解!

 
问题已经解决!只是还有点不明白,如何在浏览器里显示上传到数据库的图片?
 
上面问题都已解决,现在请教如何在severlet里弹出一个对话窗体?如何关闭原来调用该severlet的页面?解决马上结帖!
 
后退
顶部