单个文件的上传页面
多个表单进行多个文件的上传页面
Struts2配置文件:struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="i18n"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="testUpload" class="com.wang.struts2.action.UploadAction">
<result>/success.jsp</result>
</action>
<action name="testUploads" class="com.wang.struts2.action.UploadsAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
上传单个文件JSP页面:upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="testUpload" method="post" enctype="multipart/form-data">
<s:file name="file" label="File"></s:file>
<s:textfield name="FileDesc" label="FDesc"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
单个文件上传Action文件:UploadAction.java
package com.wang.struts2.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
/** * */
private static final long serialVersionUID = 1L;
private File file; //文件对应的File对象
private String fileContentType; //文件类型
private String fileFileName; //文件名
private String fileDesc;
//get和set方法
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileDesc() {
return fileDesc;
}
public void setFileDesc(String fileDesc) {
this.fileDesc = fileDesc;
}
@Override
public String execute() throws Exception {
System.out.println(file);
System.out.println(fileContentType);
System.out.println(fileFileName);
System.out.println(fileDesc);
ServletContext servletContext = ServletActionContext.getServletContext();
String dir = servletContext.getRealPath("/files/" + fileFileName);
System.out.println(dir);
//输出流
FileOutputStream out = new FileOutputStream(dir);
//输入流
FileInputStream in = new FileInputStream(file);
//缓冲区
byte [] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
//关闭输入输出流
out.close();
in.close();
return super.execute();
}
}
上传单个文件JSP页面:uploads.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="testUploads" method="post" enctype="multipart/form-data">
<s:file name="file" label="File1"></s:file>
<s:textfield name="fileDesc[0]" label="File1Desc"></s:textfield>
<s:file name="file" label="File2"></s:file>
<s:textfield name="fileDesc[1]" label="File2Desc"></s:textfield>
<s:file name="file" label="File3"></s:file>
<s:textfield name="fileDesc[2]" label="File3Desc"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
多个文件上传Action文件:UploadsAction.java
package com.wang.struts2.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadsAction extends ActionSupport {
/** * */
private static final long serialVersionUID = 1L;
private List<File> file;
private List<String> fileContentType;
private List<String> fileFileName;
private List<String> fileDesc;
@Override
public String execute() throws Exception {
System.out.println(file);
System.out.println(fileContentType);
System.out.println(fileFileName);
System.out.println(fileDesc);
ServletContext servletContext = ServletActionContext.getServletContext();
if (file != null && file.size() >0) {
for(int i = 0; i < file.size(); i++) {
System.out.println(servletContext.getRealPath("/files/" + fileFileName.get(i)));
FileOutputStream out = new FileOutputStream(servletContext.getRealPath("/files/" + fileFileName.get(i)));
FileInputStream in = new FileInputStream(file.get(i));
byte [] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
}
}
return super.execute();
}
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileDesc() {
return fileDesc;
}
public void setFileDesc(List<String> fileDesc) {
this.fileDesc = fileDesc;
}
}
上传成功页面:success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>Success Page</h4>
</body>
</html>