图片上传到服务器,数据库记录路径<%@ page contentType="text/html; charset=gb2312" language="java" import="com.jspsmart.upload.*,java.sql.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<% 
 try{mySmartUpload.initialize(pageContext);String path="no photo";
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (!myFile.isMissing()){
path="/news/newsimages/"+myFile.getFileName();
myFile.saveAs(path);}
String cmd="insert into news(content,topic,dateandtime,image,hits) values('";int j=1;
java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames();
while (e.hasMoreElements()) { String key = (String)e.nextElement();
String[] values = mySmartUpload.getRequest().getParameterValues(key);

for(int i = 0; i < values.length; i++) {
  if(j!=1) cmd+=values[i]+"','";
  }
j++;

}
java.util.Date d=new java.util.Date();
java.text.SimpleDateFormat dformat=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateandtime=dformat.format(d);
cmd+=dateandtime+"','"+path+"',0)"; //inert into news
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
          Connection   con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433","sa","sa");
               DatabaseMetaData conMD = con.getMetaData(); 
               con.setCatalog("bbs");
            Statement   stmt = con.createStatement();  
stmt.executeUpdate(cmd);
stmt.close();
con.close();
out.println("<A href='../index.jsp'>返回首页</a>");
}
catch(Exception e)
{out.println(e);
}
%>
</body>
</html>

解决方案 »

  1.   

    用com.jspsmart.upload.SmartUpload(加入classpath)包。非常方便。//upload.htm
    <form action="upload.jsp" method=post>
    <input type=file>
    <input type=submit>
    </form>//upload.jsp
    <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
    <%
        mySmartUpload.initialize(pageContext);
        mySmartUpload.upload();
       mySmartUpload.save("/");
    %> 
    文件就上传到了tomcat的root目录下(假设你没改动服务器配制)。
      

  2.   

    不用那么复杂,直接调用java API
    com.oreilly.servlet.MultipartRequest mr = new com.oreilly.servlet.MultipartRequest(request,filedir,5*1024*1024,"GB2312");
    一句话搞定。
    jsp源代码如下:
    <HTML>
    <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <TITLE>上传文件</TITLE>
    </HEAD>
    <BODY bgcolor="#FFFFFF" text="#000000">
    <p>&nbsp;</p>
    <form name="form" enctype="multipart/form-data" method="post" action="/court/UploadFiles" onSubmit = "return onBeforeSubmit()">
      <table class="normal" width="456" border="0" cellspacing="1" cellpadding="0" align="center" bordercolor="#0099CC" bgcolor="#CCCCCC">
        <tr> 
          <td bgcolor="#6699CC" colspan="2" nowrap><font class="normal">请选择上传文件</font></td>
        </tr>
        <tr bgcolor="#FFFFFF"> 
          <td height="32" width="67" nowrap> 
            <div align="center"><font class="normal">文件描述</font></div>
          </td>
          <td height="32" width="386" nowrap> 
            <input class='normal'  type="text" name="WJMS" maxlength="200" size="40">
          </td>
        </tr>
        <tr bgcolor="#FFFFFF"> 
          <td height="32" width="67" nowrap> 
            <div align="center"><font class="normal">文号</font></div>
          </td>
          <td height="32" width="386" nowrap> 
            <input class='normal'  type="text" name="WH" maxlength="200" size="20">
          </td>
        </tr>
        <tr bgcolor="#FFFFFF"> 
          <td height="26" width="67" nowrap> 
            <div align="center"><font class="normal">上传文件</font></div>
          </td>
          <td height="26" width="386" nowrap> 
            <input class='normal'  type="file" name="FILE" size="20" maxlength="200">
          </td>
        </tr>
        <tr bgcolor="#FFFFFF"> 
          <td height="29" colspan="2"> 
            <div align="center"> 
              <input class='normal'  name = "rs_add" type = "image" src="/court/renshi/images/rs_scwj.gif" >
            </div>
          </td>
        </tr>
      </table>
    </form>
    </BODY>
    </HTML>
      

  3.   

    如果你的编译器不识别这个api就去http://www.oreilly.com/下载一个
    免费的cos.jar加到classpath里面就可以了。
    还有特别注意的是<form name="form" |||enctype="multipart/form-data" |||
      

  4.   

    这个api的地址应该是http://www.servlets.com/cos/index.html