一尘不染

使用Apache Commons文件上载API在Jsp中上载多个文件

jsp

我无法上传多个文件。我正在使用此代码。这里itr.hasNext()返回false。

FileUpload.jsp

<%@ page import="java.util.*" %>
   <%@ page import="java.util.Iterator" %>
   <%@ page import="java.io.File" %>
   <%@ page import="org.apache.commons.fileupload.servlet.*" %>
   <%@ page import="org.apache.commons.fileupload.disk.*"%>
   <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
   <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
   <%@ page import="org.apache.commons.fileupload.*"%>
   <%@ page contentType="text/html;charset=UTF-8" language="java" %>
   <center>
<table border="2">
        <tr>
        <td>
        <h1>Your files  uploaded </h1>
        </td>
        </tr>
   <%
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    System.out.println("isMultipart="+isMultipart);
    System.out.println(config.getServletContext());

           FileItemFactory factory = new DiskFileItemFactory();
           ServletFileUpload upload = new ServletFileUpload(factory);
           System.out.println(upload.parseRequest(request));    
           List items=null;
           try {
                items = upload.parseRequest(request);
           } catch (FileUploadException e) {
                   e.printStackTrace();
           }
           Iterator itr = items.iterator();
           while (itr.hasNext()) {
           FileItem item = (FileItem)(itr.next());
           out.println("itr");
           if (item.isFormField()) {
                    try{
                        String field=item.getFieldName();
                        String value=item.getString();
                        System.out.println("field="+value);
                    }
                    catch(Exception e){}
           } 
           else {
                   try {
                       out.println("nor done");
                           String itemName = item.getName();
                           out.println("1done");
                           File savedFile = new File("/home/saurabh/assignments/"+itemName);
                           item.write(savedFile);  
                           out.println("done");
                   } catch (Exception e) {
                           e.printStackTrace();
                   }
           }
           }

   %>
    </table>
   </center>

html文件是:-

<html>
 <head><title>Upload page</title></head></p> <p><body>
 <form action="FileUpload.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
   <center>
   <table border="2">
       <tr>
               <td align="center"><b>Multipale file Uploade</td>
           </tr>
       <tr>
               <td>
                       Specify file: <input name="file" type="file" id="file">
                   <td>
           </tr>
           <tr>
              <td>
                     Specify file:<input name="file" type="file" id="file">
                  </td>
        <tr>
                   <td>
                      Specify file:<input name="file" type="file" id="file">
                   </td>
                 </tr>
                 <tr>
                    <td align="center">
               <input type="submit" name="Submit" value="Submit files"/>
                        </td>
                 </tr>
    </table>
        <center>
 </form>
 </body>
 </html>

这是怎么引起的,我该如何解决?


阅读 246

收藏
2020-06-10

共1个答案

一尘不染

您正在两次解析请求。第一次打印项目,第二次真正获取要处理的项目。这行不通。它只能解析一次。

删除以下无用的行

System.out.println(upload.parseRequest(request));

如果您确实出于某种原因打算打印已解析的项目,则应该这样做

System.out.println(items);

该请求中解析了一次请求之后try


具体问题 无关 ,JSP并不是从事这项工作的最佳地点。在servlet中完成此工作,并将结果收集到放置在请求范围内的某些map /
bean中,然后将请求转发到JSP以显示结果。

2020-06-10