识别多个文件上传中的每个字段 [英] Identifying each field in Multiple File upload

查看:151
本文介绍了识别多个文件上传中的每个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图用 ArrayList 与Struts2上传多个文件时,如何识别每个字段?例如,如果我有两个文件字段, File1 File2 ,在客户端,我选择只上载 File2 ,Struts 2只在列表中创建一个元素,而我无法正确映射 File1 为空, File2 与上传的文件。



有没有可以在这里使用的标识符?

解决方案

自定义对象的列表(或地图),包含您的文件,文件名和内容类型(最终是其他字段)。一个JSP 对应的元素,要清楚。

我这样做(因为我也有很多其他的字段),它的工作原理就像魅力。



(这不是唯一的方法,它只是一种工作方式,当你需要的时候会变得更加方便来处理额外的字段)



POJO

  public class MyCustomRow implements可序列化{

私有文件fileUpload;
private String fileUploadFileName;
private String fileUploadContentType;

private String otherField1;
private String otherField2;

/所有Getters和Setter在这里* /
}

in Action

  private List< MyCustomRow>行; 
* Getter和Setter ... * /

在JSP中

 < s:iterator value =rowsstatus =row> 



< / s:iterator>

文件名和内容类型将被Struts2自动检测和填充(确保您的FileUploadInterceptor堆栈)。


When trying to upload multiple files with Struts2 using ArrayList, how to identify each field?

For example, if I have two file fields, File1 and File2 and on the client side, I choose to upload only File2, Struts 2 only creates one element in the list and I am not able to correctly map File1 as empty and File2 with the uploaded file.

Is there any identifier I can use here?

解决方案

Use a List (or Map) of a custom object, containing your File, the fileName and the contentType (and eventually other fields). An JSP row corresponding element, to be clear.

I did like that (because I had many other fields too) and it works like a charm.

(this is not the only way, it is just one of the working ways that will become even more handy when you will need to handle additional fields too)

POJO

public class MyCustomRow implements Serializable {

    private File   fileUpload;
    private String fileUploadFileName; 
    private String fileUploadContentType;

    private String otherField1;
    private String otherField2;

    /* All Getters and Setters here */
}

in Action

private List<MyCustomRow> rows;
/* Getter and Setter ... */

in JSP

<s:iterator value="rows" status="row">

    <s:file      name="rows[%{#row.index}].fileUpload" />

    <s:textfield name="rows[%{#row.index}].otherField1"/>
    <s:textfield name="rows[%{#row.index}].otherField2"/>

</s:iterator>

File name and content-type will be automatically detected and populated by Struts2 (ensure you have FileUploadInterceptor in your stack).

这篇关于识别多个文件上传中的每个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆