struts2配置文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
- "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="front" namespace="/" extends="struts-default">
- <action name="upload" class="action.UplodeAction" method="upload">
- <param name="savePath">/uploadFile</param>
- <result>/success.jsp</result>
- </action>
- </package>
- </struts>
action配置
- package action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class UplodeAction extends ActionSupport {
- private String title;
- private File[] upload;
- private String[] uploadContentType;
- private String[] uploadFileName;
- private String savePath;
- @Override
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- return super.execute();
- }
- public String upload() throws Exception {
- System.out.println("upload=============");
- for (int i = 0; i < upload.length; i++) {
- System.out.println("======================="+i);
- FileInputStream fis = new FileInputStream(upload[i]);
- FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
- + getTitle()+uploadFileName[i]);
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = fis.read(buffer)) > 0) {
- fos.write(buffer, 0, len);
- }
- fos.close();
- fis.close();
- }
- return SUCCESS;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- 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() {
- return ServletActionContext.getServletContext().getRealPath(savePath);
- }
- public void setSavePath(String savePath) {
- this.savePath = savePath;
- }
- }
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <%@taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.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>
- <s:form action="/upload" enctype="multipart/form-data">
- <s:textfield name="title" label="文件名称" />
- <s:file name="upload" label="选择文件" />
- <s:file name="upload" label="选择文件" />
- <s:file name="upload" label="选择文件" />
- <s:submit value="开始上传" />
- </s:form>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <%@taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'success.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>
- 上传成功!
- <br /> 文件标题:
- <s:property value="title" />
- <br /> 文件为:
- <s:iterator value="uploadFileName" var="fileName">
- <img src="<s:property value="'uploadFile/'+title+ #fileName"/>" />
- </s:iterator>
- <s:debug />
- </body>
- </html>
web.xml文件配置
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
- <display-name>Struts2Uplode</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <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>
- </web-app>