struts2实现多文件上传

struts2配置文件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE struts PUBLIC  
  4.     "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"  
  5.     "http://struts.apache.org/dtds/struts-2.1.dtd">  
  6.   
  7. <struts>  
  8.   
  9.   
  10.     <package name="front" namespace="/" extends="struts-default">  
  11.         <action name="upload" class="action.UplodeAction" method="upload">  
  12.             <param name="savePath">/uploadFile</param>  
  13.             <result>/success.jsp</result>  
  14.         </action>  
  15.     </package>  
  16.   
  17.   
  18. </struts>  

action配置

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package action;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6.   
  7. import org.apache.struts2.ServletActionContext;  
  8.   
  9. import com.opensymphony.xwork2.ActionSupport;  
  10.   
  11. public class UplodeAction extends ActionSupport {  
  12.   
  13.     private String title;  
  14.     private File[] upload;  
  15.     private String[] uploadContentType;  
  16.     private String[] uploadFileName;  
  17.     private String savePath;  
  18.   
  19.     @Override  
  20.     public String execute() throws Exception {  
  21.         // TODO Auto-generated method stub  
  22.         return super.execute();  
  23.     }  
  24.   
  25.     public String upload() throws Exception {  
  26.   
  27.         System.out.println("upload=============");  
  28.   
  29.           
  30.           
  31.         for (int i = 0; i < upload.length; i++) {  
  32.             System.out.println("======================="+i);  
  33.             FileInputStream fis = new FileInputStream(upload[i]);  
  34.   
  35.             FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"  
  36.                     + getTitle()+uploadFileName[i]);  
  37.   
  38.             byte[] buffer = new byte[1024];  
  39.   
  40.             int len = 0;  
  41.             while ((len = fis.read(buffer)) > 0) {  
  42.                 fos.write(buffer, 0, len);  
  43.             }  
  44.   
  45.             fos.close();  
  46.             fis.close();  
  47.         }  
  48.   
  49.         return SUCCESS;  
  50.   
  51.     }  
  52.   
  53.       
  54.   
  55.       
  56.   
  57.     public String getTitle() {  
  58.         return title;  
  59.     }  
  60.   
  61.     public void setTitle(String title) {  
  62.         this.title = title;  
  63.     }  
  64.   
  65.     public File[] getUpload() {  
  66.         return upload;  
  67.     }  
  68.   
  69.     public void setUpload(File[] upload) {  
  70.         this.upload = upload;  
  71.     }  
  72.   
  73.     public String[] getUploadContentType() {  
  74.         return uploadContentType;  
  75.     }  
  76.   
  77.     public void setUploadContentType(String[] uploadContentType) {  
  78.         this.uploadContentType = uploadContentType;  
  79.     }  
  80.   
  81.     public String[] getUploadFileName() {  
  82.         return uploadFileName;  
  83.     }  
  84.   
  85.     public void setUploadFileName(String[] uploadFileName) {  
  86.         this.uploadFileName = uploadFileName;  
  87.     }  
  88.   
  89.     public String getSavePath() {  
  90.         return ServletActionContext.getServletContext().getRealPath(savePath);  
  91.     }  
  92.   
  93.     public void setSavePath(String savePath) {  
  94.         this.savePath = savePath;  
  95.     }  
  96.   
  97. }  
index页面即上传界面的设置
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"  
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8. <%@taglib prefix="s" uri="/struts-tags"%>  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11. <head>  
  12. <base href="<%=basePath%>">  
  13.   
  14. <title>My JSP 'index.jsp' starting page</title>  
  15. <meta http-equiv="pragma" content="no-cache">  
  16. <meta http-equiv="cache-control" content="no-cache">  
  17. <meta http-equiv="expires" content="0">  
  18. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19. <meta http-equiv="description" content="This is my page">  
  20. <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23. </head>  
  24.   
  25. <body>  
  26.     <s:form action="/upload" enctype="multipart/form-data">  
  27.         <s:textfield name="title" label="文件名称" />  
  28.         <s:file name="upload" label="选择文件" />  
  29.         <s:file name="upload" label="选择文件" />  
  30.         <s:file name="upload" label="选择文件" />  
  31.         <s:submit value="开始上传" />  
  32.     </s:form>  
  33.   
  34. </body>  
  35. </html>  
上传成功的设置

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"  
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8. <%@taglib prefix="s" uri="/struts-tags"%>  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11. <head>  
  12. <base href="<%=basePath%>">  
  13.   
  14. <title>My JSP 'success.jsp' starting page</title>  
  15.   
  16. <meta http-equiv="pragma" content="no-cache">  
  17. <meta http-equiv="cache-control" content="no-cache">  
  18. <meta http-equiv="expires" content="0">  
  19. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20. <meta http-equiv="description" content="This is my page">  
  21. <!-- 
  22.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  23.     -->  
  24.   
  25. </head>  
  26.   
  27. <body>  
  28.     上传成功!  
  29.     <br /> 文件标题:  
  30.     <s:property value="title" />  
  31.     <br /> 文件为:  
  32.     <s:iterator value="uploadFileName" var="fileName">  
  33.          <img src="<s:property value="'uploadFile/'+title+ #fileName"/>/>   
  34.     </s:iterator>  
  35.   
  36.     <s:debug />  
  37. </body>  
  38. </html>  

web.xml文件配置


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>Struts2Uplode</display-name>  
  4.    <welcome-file-list>  
  5.     <welcome-file>index.jsp</welcome-file>  
  6.   </welcome-file-list>  
  7.   <filter>  
  8.       <filter-name>struts2</filter-name>  
  9.       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  10.   </filter>  
  11.   <filter-mapping>  
  12.     <filter-name>struts2</filter-name>  
  13.     <url-pattern>/*</url-pattern>  
  14.   </filter-mapping>  
  15. </web-app>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值