有没有设置 form 的 multipart 属性啊?

解决方案 »

  1.   

    我是照例子作的:
    http://www.csdn.net/Develop/read_article.asp?id=18987
    没有错误
    就是取不出文件?????????????
      

  2.   

    选择上传文件的页面如下:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <link href="css/default.css" rel="stylesheet" type="text/css">
    </head>
    <script language="javascript">
    function check_submit(form) {
      if (form.hasPic.value == 1 && form.pic.value == "") {
        alert("请输入档案");
        return false;
      }  return true;
    }
    </script><body bgcolor="#189ECE" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
    <a name="top"></a>
    <form name="myform" action="file_save.jsp" ENCTYPE="multipart/form-data" method="post" onsubmit="return check_submit(this)">
    <input type="hidden" name="news_id" value="<%=request.getParameter("news_id")%>">
    <input type="hidden" name="hasPic" value="1">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      
                <br>
                <table width="100%" border="0" cellspacing="0" cellpadding="5">
                    <tr>
                      <td align="right" valign="top">图片上传</td>
                      <td><input type="file" name="pic">(档案的大小不能超过10k)</td>
                    </tr>
                    <tr align="center">
                      <td colspan="2"> <input type="submit" name="Submit" value="确定上传">
                        <input type="button" value="不加图片" onclick="document.myform.hasPic.value = 0; document.myform.submit()">             
                      </td>
                    </tr>
                </table>
                <p>&nbsp;</p>
                <!-- InstanceEndEditable --></td>
            </tr>
          </table></td>
        <td bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
      
    </table>
    </form>
    <p>&nbsp;</p></body>
    <!-- InstanceEnd --></html>
    保存文件的页面:
    <%-- *********************************** page directive ********************************** --%><%@ page import="java.util.Date" %>
    <%@ page import="com.jspsmart.upload.*" %>
    <%@ page import="java.io.File" %>
    <%-- ************************************************************************************* --%>
    <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
    <%
    // upload the image file from user's computer
    mySmartUpload.initialize(pageContext);
    mySmartUpload.upload();
    int fileSize = mySmartUpload.getSize();
    com.jspsmart.upload.Request jspRequest = mySmartUpload.getRequest();
    %>
    <form name="myform" action="generate_news.jsp" method="post">
    <input type="hidden" name="news_id" value="<%=jspRequest.getParameter("news_id")%>">
    <input type="hidden" name="pic" value="">
    </form>
    <%
    if(fileSize > 15360) {
      // file size greater than expected
    %>
    <script language="javascript">
    alert("抱歉!文件超过15k");
    document.myform.action = "前一页面.jsp";
    document.myform.submit();
    </script>
    <%
    } else if(mySmartUpload.getFiles().getCount() > 0) {
      // save it to the disk and get the file name that it is saved as
      int count = mySmartUpload.save("tmp");
      com.jspsmart.upload.File tmpFile = mySmartUpload.getFiles().getFile(0);
      String fileName = tmpFile.getFileName();
      String ext = tmpFile.getFileExt();
      java.io.File oldFile = new java.io.File("tmp/" + fileName);
      String path = oldFile.getAbsolutePath();
      path = path.substring(0, path.indexOf("/tmp"));
      
      Process proc = Runtime.getRuntime().exec("mv " + application.getInitParameter("HOMEPATH") + "tmp/" + fileName + " " + application.getInitParameter("HOMEPATH") + "generated_pages/images/news_" + jspRequest.getParameter("news_id") + "." + ext);
    %>
    <script language="javascript">
    document.myform.pic.value = "<%=(jspRequest.getParameter("news_id") + "." + ext)%>";
    document.myform.submit();
    </script>
    <%
    } else {
    %>
    <script language="javascript">
      document.myform.submit();
    </script>
    <%
    }
    %>