Struts2多文件上传

1)前台代码:

<form action="fileupload2.action" 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">
    	
    	
    </form>


2)struts.xml配置:

<action name="fileupload2" class="com.wildcat.Action.fileupload2">
				<result name="success">/FileUpload/fileuploadResult2.jsp</result>
			</action>
		

3)action内容:

package com.wildcat.Action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class fileupload2 extends ActionSupport {

	private String username;
	//file定义成一个集合
	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++){
			//指定上传路径
		String root=ServletActionContext.getRequest().getRealPath("/upload");
		//输入流
		 InputStream is=new FileInputStream(file.get(i));
			
			System.out.println("FileName: "+fileFileName);
			byte [] buffer=new byte[400];
			//输出流
			OutputStream os=new FileOutputStream(new File(root,fileFileName.get(i)));
			int length=0;
			while (-1!=(length=is.read(buffer))){
				
				os.write(buffer, 0, length);
			}
		is.close();
		os.close();
		}
	
		return SUCCESS;
	}
	
}

4)结果页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'fileuploadResult2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    username:<s:property value="username"/><br>
    <!-- 使用struts2标签库 循环 -->
    <s:iterator value="fileFileName" id="f">
    	filename:<s:property value="#f"/>
   
    </s:iterator>
    
  </body>
</html>

原文地址:https://www.cnblogs.com/lixingle/p/3312998.html