群号:15328132       (为了不让无关人员进入,请回答一个简单的问题)
我有一个上传的文件和一个插入数据库的文件,想把它们合并,结果出了问题,上传可以,插入错无!<!-- *********** upload.htm ************ -->
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            <!-- saved from 
            url=(0057)http://localhost:8080/jspsmartfile/jsp/uploadTemplate.jsp 
            -->
            <HTML><HEAD>
            <META content="text/html; charset=gb2312" http-equiv=Content-Type>
            <META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
            <BODY bgColor=#e6e6e6><BR>
            <FORM action="upload.jsp"  encType=multipart/form-data method=post>
            <TABLE align="center">
              <TBODY>
              <TR>
                <TD width="733"><p><FONT color=#000000 face=helv,helvetica>文件1:</FONT>
                    <INPUT  name="file1" type=file id="file1"  size=60>
                </p>
                <p>
                  文件2:
                  <INPUT  name="file2" type=file id="file"  size=60> 
                  </p>
  <p>&nbsp;                  </p>
         </TD>
              </TR>
               <tr><td>              </p>
   </td>
               </tr>
              <TR>
                <TD align=right>
<INPUT type=submit value=提交 name="send">
</TD>
</TR>
</TBODY>
</TABLE>
</FORM>
</BODY>
</HTML>
<!-- ***********upload.jsp ************ -->
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.io.*"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
            
//创建文件存储路径
java.io.File parentDoc=new java.io.File("C:/Apache2/htdocs/jspPic");
String []fList=parentDoc.list();
String  fName=null;
java.io.File createFile=new java.io.File("C:/Apache2/htdocs/jspPic");
boolean flag=false;
boolean mdFlag = false;

                com.jspsmart.upload.SmartUpload mySmartUpload=new  com.jspsmart.upload.SmartUpload();
                mySmartUpload.initialize(pageContext);
                mySmartUpload.setMaxFileSize(500*1024*1024);  //文件最大大小为500M
                mySmartUpload.upload();
               for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
   {
   com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
   if (!myFile.isMissing())
{
     try{
  String myFileName=myFile.getFileName();
  String  suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
  String  ext= mySmartUpload.getFiles().getFile(0).getFileExt();
  int fileSize=myFile.getSize();
  String aa=getServletContext().getRealPath("/")+"jspPic\\";
  String trace=aa+myFileName;
  out.println("文件"+(i+1)+"已成功上传于: /jsppic/"+myFileName+"<br>"); //存储路径!
  String   explain=(String)mySmartUpload.getRequest().getParameter("text");
  String   send=(String)mySmartUpload.getRequest().getParameter("send");
  myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
    }catch(Exception e){
                      e.printStackTrace();
                         }
       } else   {
   out.println(("文件"+(i+1)+"没有选择文件!").toString());
   }
               }//与前面的for对应
   
   
   
%>
<HTML> <!-- *********** PrepInsert.html ************ -->
<BODY>
<FORM ACTION="/PrepInsert.jsp" METHOD="post">
  <p>用户名:
    <INPUT TYPE="text" NAME="auth"  SIZE="12" align="right">
    <BR>
电 话:
<INPUT TYPE="text" NAME="tel"   SIZE="12" align="right">
 
  </p>
  <p>文件:
    <INPUT  name="file1" type=file id="file1"  size=60>
    <br>
    <INPUT TYPE="submit" NAME="submit" VALUE="提交">
 
    </p>
</FORM>
</HTML>
<HTML> <!-- **************** PrepInsert.jsp *************** -->
<BODY>
<%@ page import="java.sql.*"
         contentType="text/html;charset=gb2312" %>
<%
Connection con = null;
String auth=request.getParameter("auth");
String tel=request.getParameter("tel");
String file=request.getParameter("file1");
try
  {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  con=DriverManager.getConnection("jdbc:odbc:user_db","sa","");
  String sql = "insert into aa values (?,?);";
  PreparedStatement pstmt = con.prepareStatement(sql);
  pstmt.setString(1, auth);
  pstmt.setString(2, file);
  pstmt.executeUpdate();
  //out.println(+file+);
  out.println("insert into aa values ('" + auth + "','" +
              tel + "');" + "<BR>prepinsert OK!!<BR>");
  pstmt.close();
  con.close();
  }
catch (Exception e)
  {
  out.println(e.getMessage());
  }
  
%>
</BODY>
</HTML>

解决方案 »

  1.   

    合并后的代码:
    <!-- *********** upload.htm ************ -->
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
                <!-- saved from 
                url=(0057)http://localhost:8080/jspsmartfile/jsp/uploadTemplate.jsp 
                -->
                <HTML><HEAD>
                <META content="text/html; charset=gb2312" http-equiv=Content-Type>
                <META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
                <BODY bgColor=#e6e6e6><BR>
                <FORM action="upload.jsp"  encType=multipart/form-data method=post>
                <TABLE align="center">
                  <TBODY>
                  <TR>
                    <TD width="733"><p><FONT color=#000000 face=helv,helvetica>文件1:</FONT>
                        <INPUT  name="file1" type=file id="file1"  size=60>
                    </p>
                    <p>
                      文件2:
                      <INPUT  name="file2" type=file id="file"  size=60> 
                      </p>
      <p>用户名:
        <INPUT TYPE="text" NAME="auth"  SIZE="12" align="right">
        <BR>
    电 话:
    <INPUT TYPE="text" NAME="tel"   SIZE="12" align="right">
     
      </p>
      <p>&nbsp;                  </p>
             </TD>
                  </TR>
                   <tr><td>              </p>
       
       </td>
                   </tr>
                  <TR>
                    <TD align=right>
    <INPUT type=submit value=提交 name="send">
    </TD>
    </TR>
    </TBODY>
    </TABLE>
    </FORM>
    </BODY>
    </HTML><!-- ***********upload.jsp ************ -->
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.sql.*,java.io.*"%>
    <%@ page import="com.jspsmart.upload.*" %>
    <%
                
    //创建文件存储路径
    java.io.File parentDoc=new java.io.File("C:/Apache2/htdocs/jspPic");
    String []fList=parentDoc.list();
    String  fName=null;
    java.io.File createFile=new java.io.File("C:/Apache2/htdocs/jspPic");
    boolean flag=false;
    boolean mdFlag = false;

                    com.jspsmart.upload.SmartUpload mySmartUpload=new  com.jspsmart.upload.SmartUpload();
                    mySmartUpload.initialize(pageContext);
                    mySmartUpload.setMaxFileSize(500*1024*1024);  //文件最大大小为500M
                    mySmartUpload.upload();
                   for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
       {
       com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
       if (!myFile.isMissing())
    {
         try{
      String myFileName=myFile.getFileName();
      String  suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
      String  ext= mySmartUpload.getFiles().getFile(0).getFileExt();
      int fileSize=myFile.getSize();
      String aa=getServletContext().getRealPath("/")+"jspPic\\";
      String trace=aa+myFileName;
      out.println("文件"+(i+1)+"已成功上传于: /jsppic/"+myFileName+"<br>"); //存储路径!
      String   explain=(String)mySmartUpload.getRequest().getParameter("text");
      String   send=(String)mySmartUpload.getRequest().getParameter("send");
      myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
        }catch(Exception e){
                          e.printStackTrace();
                             }
           } else   {
       out.println(("文件"+(i+1)+"没有选择文件!").toString());
       }
                   }//与前面的for对应
       
       Connection con = null;
    String auth=request.getParameter("auth");
    String tel=request.getParameter("tel");
    String file=request.getParameter("file1");
    try
      {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:user_db","sa","");
      String sql = "insert into aa values (?,?);";
      PreparedStatement pstmt = con.prepareStatement(sql);
      pstmt.setString(1, auth);
      pstmt.setString(2, file);
      pstmt.executeUpdate();
      //out.println(+file+);
      out.println("insert into aa values ('" + auth + "','" +
                  tel + "');" + "<BR>prepinsert OK!!<BR>");
      pstmt.close();
      con.close();
      }
    catch (Exception e)
      {
      out.println(e.getMessage());
      }
       
       
       
    %>
    运行结果:
    文件1已成功上传于: /jsppic/flower.jsp
    文件2已成功上传于: /jsppic/savedata.jsp
    insert into aa values ('null','null');
    prepinsert OK!!能上传,不能插入!
    insert into aa values ('null','null');说明参数没接到,为什么啊?请高手指教!