【问题标题】:file upload issue in springs and struts 2.3 integrated appsprings 和 struts 2.3 集成应用程序中的文件上传问题
【发布时间】:2014-12-01 11:52:59
【问题描述】:

我的 Spring 应用程序中的多部分文件上传功能遇到问题。我在我的应用程序的其他几个 jsp 文件中使用 struts2.3 标记。在我的 spring 应用程序中将 struts 从 1.2 迁移到 2.3 之前,上传功能运行良好。

我上传的jsp文件中的代码sn-p:

<td nowrap="nowrap">
                <input type="file" name="file" class="formElement" size="40"/>&nbsp;
                <input name="actionAdd" type="button" class="button" value="Add File to Table" onclick="addFileToTable();"/>
                <div id="noFileSelected" style="display:none"><p><span class="error">No File selected.</span> </p></div>
            </td>

实现类中的代码块:

multiPartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multiPartRequest.getFile("file");

spring 配置文件中的代码

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="500000"/>
</bean>

multiPartRequest.getFile("file") 返回 null。

之前相同的代码块运行良好。

【问题讨论】:

    标签: spring file-upload struts2


    【解决方案1】:

    我猜你有带有 Spring-plugin + Spring 库的 Struts2(而不是 Spring MVC,它是 Struts2 的替代品);那为什么要使用Spring来上传文件,而Struts2更好,而且不用写代码呢?

    JSP

    <input type="file" name="file" class="formElement" size="40"/>
    

    动作

    private File file;
    private String fileFileName;
    private String fileContentType;
    
    /* Getters and Setters */
    

    确保在你的堆栈中有 FileUpload 拦截器(如果你没有接触任何东西,你有它),就是这样。全自动。

    它也是works perfectly with multiple files

    【讨论】:

      【解决方案2】:

      尝试从文件对象直接获取文件,不要使用多部分请求来获取文件。

      private List<File> file; // if you want to select multiple files
      private String fileFileName;
      private String fileContentType;
      
          for (File file : file){
      
              System.out.print("\nFile ["+i+"] ");
                            System.out.print("title:"+imgTitle);
                            System.out.print("; name:"         + fileFileName.get(i));
                            System.out.print("; contentType: " + fileContentType.get(i));
                            System.out.print("; length: "      + file.length());
          }
      

      【讨论】:

      • 如果您对文件对象使用列表,您也应该对文件名和内容类型使用列表。此外,由于您的代码与the one in this question 相同,您可以提供对原始帖子的引用。最后,不清楚 imgTitle 是什么,因为它没有在任何地方定义
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多