1 <%@ page contentType="text/html;charset=UTF-8" language="java"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <title>上传文件</title> 7 </head> 8 9 <body> 10 <s:form action="upload" enctype="multipart/form-data"> 11 <s:file name="upload" label="上传路径1"></s:file> 12 <s:file name="upload" label="上传路径2"></s:file> 13 <s:file name="upload" label="上传路径3"></s:file> 14 <s:submit value="上传"></s:submit> 15 </s:form> 16 </body> 17 </html>
package com.action;
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";
}
}
配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <constant name="struts.enable.DynamicMethodInvocation" 8 value="true" /> 9 <constant name="struts.devMode" value="false" /> 10 <include file="struts-default.xml" /> 11 12 <package name="up-load" extends="struts-default"> 13 <action name="upload" class="com.action.UploadAction"> 14 <param name="savePath">/uploadAdd</param> <!-- 部署本工程前,要在WebRoot下手工创建文件夹uploadAdd,保存的文件 --> 15 <result name="success">upsuccess.jsp</result> 16 <result name="input">uperror.jsp</result> 17 <interceptor-ref name="defaultStack"/> 18 <interceptor-ref name="fileUpload"> 19 <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> <!-- 允许上传的文件类型 --> 20 <param name="maximumSize">2048000</param> <!-- 限制上传文件大小为2048000字节 --> 21 </interceptor-ref> 22 </action> 23 24 </package> 25 </struts>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <filter> 9 <filter-name>Struts2</filter-name> 10 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 11 </filter> 12 13 <filter-mapping> 14 <filter-name>Struts2</filter-name> 15 <url-pattern>/*</url-pattern> 16 </filter-mapping> 17 18 <welcome-file-list> 19 <welcome-file>index.jsp</welcome-file> 20 </welcome-file-list> 21 </web-app>