0

好吧,我正在尝试从 Web 界面将文件上传到我的 servlet。我一直在按照教程使用 Apache Commons FileUpload 并将其集成到我的 servlet 中。不知何故,我无法设法上传文件。

这是我在网页中的表格:

<form class="well" action="GenTreeUploader" method="post" enctype="multipart/form-data">
        <label>Choose your file:</label>
        <center><input type="file" class="input-xlarge" name="wordfile"></center><br>
        <center><span class="help-block">Note: after clicking "Upload file!" all of the data contained in the file will be uploaded to the database</span></center><br>
        <center><input class="btn btn-primary" type="submit" value="Upload file!"></center>  
        <input type="text" name="tester" value="xoxoxo" />
        <input type="hidden" name="action" value="startUpload" />
    </form>

然后这是我的 servlet 代码部分,它应该处理来自请求的文件上传:

public void artiUpload(HttpServletRequest request, HttpServletResponse response, HtmlWriter writer) throws ServletException, IOException {
    System.out.println("I'm in upload");
    PrintWriter out = response.getWriter();
    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    // Create a factory for disk-based file items

    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();

    // Set factory constraints
    factory.setSizeThreshold(maxMemSize);
    factory.setRepository(new File(htmlPath.toString()));
    System.out.println("File path: " + htmlPath.toString());

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Set overall request size constraint
    upload.setSizeMax(maxFileSize);
    System.out.println("Max file size: " + maxFileSize);
    System.out.println("Max file size in memory: " + maxMemSize);
    List items = null;
    try {
        // Parse the request
        items = upload.parseRequest(request);
        System.out.println("Items found: " + items.size());
        out.write(writer.printCenter("File uploading done"));
    } catch (FileUploadException ex) {
        Logger.getLogger(GenTreeUploader.class.getName()).log(Level.SEVERE, null, ex);
        out.write(writer.printCenter("File uploading failed"));
    }
}

这是我从这个 servlet 方法得到的 Tomcat 服务器日志:

I'm in upload
File path: D:\Dropbox\StudiesNew\NetbeansProjects\GenTreeUploader\build\web\WEB-INF\classes\Html
Max file size: 10485760
Max file size in memory: 2097152
Items found: 0

好吧,我还通过使用从请求中获取了一些参数: action = request.getParameter(actionText);

正常参数下工作正常,但文件没有上传。我相信在这种情况下解析列表后应该有一个项目,它显示 0 ...

任何人都可以帮忙吗?谢谢。

完整的日志是这个?

 Using CATALINA_BASE:   "C:\Users\Arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base"
Using CATALINA_HOME:   "C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.22"
Using CATALINA_TMPDIR: "C:\Users\Arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk1.7.0_03"
Using CLASSPATH:       "C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.22\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.22\bin\tomcat-juli.jar"
Bir 05, 2012 2:45:06 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0_03\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\MATLAB\R2010b\runtime\win64;C:\Program Files\MATLAB\R2010b\bin;.
Bir 05, 2012 2:45:07 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8084"]
Bir 05, 2012 2:45:07 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Bir 05, 2012 2:45:07 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 865 ms
Bir 05, 2012 2:45:07 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Bir 05, 2012 2:45:07 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.22
Bir 05, 2012 2:45:07 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor GenTreeUploader.xml from C:\Users\Arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\Catalina\localhost
Bir 05, 2012 2:45:08 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor manager.xml from C:\Users\Arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\Catalina\localhost
Bir 05, 2012 2:45:08 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor mrweb.xml from C:\Users\Arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\Catalina\localhost
Bir 05, 2012 2:45:08 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor ROOT.xml from C:\Users\Arturas\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\Catalina\localhost
Bir 05, 2012 2:45:08 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8084"]
Bir 05, 2012 2:45:08 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Bir 05, 2012 2:45:08 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1396 ms
I'm in upload
File path: D:\Dropbox\StudiesNew\NetbeansProjects\GenTreeUploader\build\web\WEB-INF\classes\Html
Max file size: 10485760
Max file size in memory: 2097152
Items found: 0
4

1 回答 1

4

当请求正文已经通过调用getParameter(),getReader()getInputStream()在同一个 servlet 的方法内部或也在同一请求上运行的 servlet 过滤器之一的方法request内部的方法预先解析时,它将以这种方式失败。另一个可能的原因是您嵌套了 HTML元素,但行为取决于浏览器。doPost()doFilter()<form>

顺便说一句,HTML<center>元素自 1998 年以来已被弃用。摆脱它并改用 CSS。


更新:您的更新确认您正在调用getParameter(). 当它是一个请求时,request你不应该这样做。multipart/form-data您应该改为使用相同的 Apache Commons FileUpload API 来收集普通表单字段。另请参阅如何使用 JSP/Servlet 将文件上传到服务器?

于 2012-06-04T23:28:49.030 回答