一个同时把上传图片和其他字段同时写入数据库的问题!
add.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
<link href= "../hyzx/inc/stlye.css " rel= "stylesheet " type= "text/css " />
</head>
<body>
<form action= "add.jsp " method= "post " enctype= "multipart/form-data " name= "form1 " id= "form1 ">
<table width= "764 " border= "0 " align= "center " cellpadding= "0 " cellspacing= "0 " bgcolor= "#81A4D3 ">
<tr>
<td height= "23 " colspan= "2 " align= "center " background= "../images/tabbg.gif "> <strong> 发布企业信息 </strong> </td>
</tr>
</table>
<table width= "760 " border= "0 " align= "center " cellpadding= "0 " cellspacing= "1 " bgcolor= "#81A4D3 ">
<tr>
<td width= "91 " height= "30 " bgcolor= "#FFFFFF "> 选择公司形象: </td>
<td width= "669 " height= "30 " bgcolor= "#FFFFFF ">
<label>
<input type= "file " name= "file " style= "height:18px;width:160px;border-color: #81A4D3; border-style: solid; border-width: 1px 1px 1px 1px " />
</label> </td>
</tr>
<tr>
<td width= "91 " height= "30 " bgcolor= "#FFFFFF "> 公司名称 </td>
<td width= "669 " height= "30 " bgcolor= "#FFFFFF "> <input name= "company " type= "text " style= "height:18px;width:160px;border-color: #81A4D3; border-style: solid; border-width: 1px 1px 1px 1px " id= "company " /> </td>
</tr>
<tr>
<td colspan= "2 " bgcolor= "#FFFFFF "> <table width= "760 " border= "0 " align= "center " cellpadding= "2 " cellspacing= "0 ">
<tr>
<td width= "317 " align= "right " bgcolor= "#F2F5FB "> <input type= "submit " name= "Submit " value= "提交 " /> </td>
<td width= "443 " bgcolor= "#F2F5FB "> <input type= "reset " name= "Submit2 " value= "重置 " /> </td>
</tr>
</table> </td>
</tr>
</table>
</form>
</body>
</html>
add.jsp
<%@ page language= "java " pageEncoding= "GBK "%>
<%@ page import = "conn.* "%>
<%@ page import = "java.util.* "%>
<%@ page import = "java.io.* "%>
<%@ page import = "java.sql.* "%>
<%@ page import= "com.jspsmart.upload.* " %>
<%!
public String getStr(String str)
{ PrintWriter out=null;
try
{
str=new String(str.getBytes( "ISO8859_1 "), "GBK ");
}
catch(Exception e)
{
out.println(e.getMessage());
}
return str;
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
</head>
<body>
<jsp:useBean id= "db " class= "conn.DBHandle " scope= "page "> </jsp:useBean>
<%
//Integer ii=(Integer)session.getAttribute( "filename ");
// int filename=ii.intValue();
// int filename1=filename+1;
// session.setAttribute( "filename ",new Integer(filename1));
// System.out.println((((Integer)session.getAttribute( "filename ")).intValue()));
String myFileName = " ";
//实例化上载bean
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500*1024*1024);
//上载文件
mySmartUpload.upload();
//循环取得所有上载的文件
for (int i=0;i <mySmartUpload.getFiles().getCount();i++){
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上载的文件的文件名
myFileName=myFile.getFileName();
out.print(myFileName);
//取得不带后缀的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf( '. '));
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa=getServletContext().getRealPath( "/ ")+ "qy\\upload\\ ";
String trace=aa+myFileName;
//System.out.println(trace);
//取得别的参数
String explain=(String)mySmartUpload.getRequest().getParameter( "text ");
String send=(String)mySmartUpload.getRequest().getParameter( "send ");
//将文件保存在服务器端
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
//下面的是将上载的文件保存到数据库中
//将文件读到流中
out.println(( "上载成功!!! ").toString());
}
else
{ out.println(( "上载失败!!! ").toString()); }
}//与前面的if对应
String type=(String)session.getAttribute( "type ");
//System.out.println(type);
String company;
company=getStr(request.getParameter( "company "));
try
{
db.executeUpdate( "insert into corporation (pic,company) values ( ' "+myFileName+ " ', ' "+company+ " ') ");
out.print( "恭喜你,数据插入成功! <a href=index.jsp target=main> 返回 </a> ");
}
catch(Exception e)
{
out.print(e.getMessage());
}
%>
</body>
</html>
出错提示:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
org.apache.jsp.admin.EntManage.add_jsp.getStr(add_jsp.java:24)
org.apache.jsp.admin.EntManage.add_jsp._jspService(add_jsp.java:136)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
--------------------------------------------
Apache Tomcat/5.0.28
我能把上传图片上传到指定位置,也能把图片文件名写入数据库了,但如果同时把公司名称,所在区域等一起写入数据库就出错了!我查到如果有上传功能后有这样一行代码: <form action= "add.jsp " method= "post " enctype= "multipart/form-data " name= "form1 " id= "form1 ">
使所有其他的取值为null,比如:公司名称:company=getStr(request.getParameter( "company "));结果company为null!希望热心朋友帮我解决这个同时能上传图片有能把其他取值一块写入数据库的问题!多谢各位朋友了!
[解决办法]
把 enctype= "multipart/form-data " 去掉试试
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html