Struts多文件上传
2014-03-04 10:36
393 查看
struts2配置文件
Action配置
index页面即上传界面的设置
上传成功的设置
web.xml文件配置
<?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 { return super.execute(); } public String upload() throws Exception { for (int i = 0; i < upload.length; 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; } }
index页面即上传界面的设置
<%@ 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"> </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"> </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>
相关文章推荐
- [转]采用dwr+ajax和struts开发文件上传进度条
- Struts 2读书笔记-----使用Struts 2控制文件上传
- Struts 1.3.10 上传文件时发生问题。1.3.8,1.2.9没有类似问题
- Struts 的文件上传组件org.apache.struts.upload
- struts(八)文件上传
- Struts处理文件上传
- Struts2.x上传和下载文件
- 采用dwr+ajax和struts开发文件上传进度条(转)
- struts2单文件上传
- Struts学习笔记: 实现文件上传下载
- Struts2多文件上传
- struts文件上传、文件下载
- struts 实现文件上传与下载
- 简易版 文件上传UpdateFile(Struts2版)
- struts2.X心得8--struts与servlet的松耦合scope之间的访问以及文件上传
- struts中上传多个文件
- Struts实现自动多文件上传
- [精]采用dwr+ajax和struts开发文件上传进度条
- Struts实现多个文件上传(动态添加数量、前台仿163邮箱)
- Struts文件上传