Struts2 多文件上传
UploadAction.java 文件
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">package com.action;</span>
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
private File[] upload; //封装上传文件
private String[] uploadContentType; //封装上传文件类型
private String[] uploadFileName; //封装上传文件名
private String savePath; //封装上传文件在服务器tomcat容器上的保存路径
private FileInputStream fileInput;
private FileOutputStream fileOutput;
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath()
{
//以下ServletActionContext.getServletContext().getRealPath(“”)方法获取本工程在tomcat容器中的真实绝对路径,加上参数savepath,进一步得到下一级savepath的绝对路径
String realpath = ServletActionContext.getServletContext().getRealPath(savePath);
return realpath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute() throws Exception
{
System.out.println("这是uploadAction");
File[] files =this.getUpload();
this.setSavePath("");
for(int i=0;i<files.length;i++){
fileOutput=new FileOutputStream(this.getSavePath()+ "\\" + this.getUploadFileName()[i]);
fileInput=new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len=0;
while((len=fileInput.read(buffer)) > 0 ){
fileOutput.write(buffer,0,len);
}
}
return "success";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>上传文件</title>
</head>
<body>
<s:form action="upload" enctype="multipart/form-data">
<s:file name="upload" label="上传路径1"></s:file>
<s:file name="upload" label="上传路径2"></s:file>
<s:file name="upload" label="上传路径3"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="true" />
<constant name="struts.devMode" value="false" />
<include file="struts-default.xml" />
<package name="up-load" extends="struts-default">
<action name="upload" class="com.action.UploadAction">
<param name="savePath">/uploadAdd</param> <!-- 部署本工程前,要在WebRoot下手工创建文件夹uploadAdd,保存的文件 -->
<result name="success">upsuccess.jsp</result>
<result name="input">uperror.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/gif,image/jpeg,image/x-png,image/bmp,text/plain,application/msword,application/vnd.ms-excel,application/x-zip-compressed,application/pdf,application/vnd.ms-powerpoint</param> <!-- 允许上传的文件类型 -->
<param name="maximumSize">2048000</param> <!-- 限制上传文件大小为2048000字节 -->
</interceptor-ref>
</action>
</package>
</struts>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>上传文件成功</title>
</head>
<body vLink="#006666" link="#003366" bgColor="#E0F0F8">
<h3>上传文件成功:</h3>
<s:property value="uploadFileName"/><br><br>
<a href="index.jsp">继续上传</a>
</body>
</html>