Struts2 多文件上传

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

1、建一个上传页面multiUpload.jsp


<body>
    <h4>this is the fileupload2.jsp for many file</h4>
    <form action="fileupload" method="post" enctype="multipart/form-data">
    	username : <input type="text" name="username" ><br>
    	file1 : <input type="file" name="file"><br>
    	file2 : <input type="file" name="file"><br>
    	file3 : <input type="file" name="file"><br>
    	<input type="submit" value="submit"><br> 
    </form>
  </body>


2、写对应的action处理类:

public class UpLoad2Action extends ActionSupport {

	private String username;
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;
	

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	@Override
	public String execute() throws Exception {
		for (int i = 0; i < file.size(); i++) {
			InputStream is = new FileInputStream(file.get(i));
			String root = ServletActionContext.getRequest().getRealPath("/upload");
			File dest = new File(root, fileFileName.get(i));
			OutputStream os = new FileOutputStream(dest);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				os.write(buffer, 0, len);
			}
			is.close();
			os.close();
		}
		return SUCCESS;
	}
}

3、配置struts.xml文件:

<action name="fileupload" class="com.strong.action.UpLoad2Action">
			<result name="success">/present2.jsp</result>
		</action>


4、显示页面:

<body>
    <h4>this is the fileUploadResult2.jsp</h4>
     username : <s:property value="username"/><br>
     <!-- 迭代去取上传文件的名字 ,-->
     <s:iterator value="fileFileName" id="f">
    	 fileName: <s:property value="#f"/><br>
     </s:iterator>
  </body>


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:TextView实现跑马灯效果

下一篇:Android子线程更新UI两种方法