struts2文件上传

 1.UploadAction代码:多文件上传

  1. package com.hxz.action;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import org.apache.struts2.ServletActionContext;
  8. import com.hxz.exception.NameException;
  9. import com.opensymphony.xwork2.ActionContext;
  10. import com.opensymphony.xwork2.ActionSupport;
  11. @SuppressWarnings("serial")
  12. public class UploadAction extends ActionSupport {
  13.     private String title;
  14.     private File[] upload;
  15.     private String[] uploadContentType;
  16.     private String[] uploadFileName;
  17.     private String savePath;
  18.     
  19.     
  20.     public File[] getUpload() {
  21.         return upload;
  22.     }
  23.     public void setUpload(File[] upload) {
  24.         this.upload = upload;
  25.     }
  26.     public String[] getUploadContentType() {
  27.         return uploadContentType;
  28.     }
  29.     public void setUploadContentType(String[] uploadContentType) {
  30.         this.uploadContentType = uploadContentType;
  31.     }
  32.     public String[] getUploadFileName() {
  33.         return uploadFileName;
  34.     }
  35.     public void setUploadFileName(String[] uploadFileName) {
  36.         this.uploadFileName = uploadFileName;
  37.     }
  38.     public String getSavePath() throws Exception {
  39.         return ServletActionContext.getRequest().getRealPath(savePath);
  40.     }
  41.     public void setSavePath(String savePath) {
  42.         this.savePath = savePath;
  43.     }
  44.     public String getTitle() {
  45.         return title;
  46.     }
  47.     public void setTitle(String title) {
  48.         this.title = title;
  49.     }
  50.     public String execute() throws Exception {
  51.         File[] files = getUpload();
  52.         for(int i=0;i<files.length;i++){
  53.         try {
  54.             InputStream is = new FileInputStream(files[i]);
  55.             File file = new File(this.getSavePath(), this.getUploadFileName()[i]);
  56.             OutputStream os = new FileOutputStream(file);
  57.             byte[] buffer = new byte[1024];
  58.             int len = 0;
  59.             while ((len = is.read(buffer)) > 0) {
  60.                 os.write(buffer, 0, len);
  61.             }
  62.             os.close();
  63.             is.close();
  64.         } catch (Exception e) {
  65.             e.printStackTrace();
  66.             throw new NameException("上传文件错误!");
  67.         }
  68.         }
  69.         return "zhe";
  70.     }
  71. }

 

2.upload.jsp

  1. <%@ page language="java"  pageEncoding="GBK"%>
  2. <%@ taglib prefix ="s" uri="/struts-tags" %>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5.   <head>
  6.     
  7.     <title>Upload File</title>
  8.     
  9.   </head>
  10.   
  11.   <body>
  12.   
  13.     <form action="uploadMany.action" method="post" enctype="multipart/form-data">
  14.      <s:fielderror/>
  15.      ${requestScope.typeError} <br>
  16.       文件标题:<input type="text" name="title"/><br>
  17.       选择1文件:<input type="file" name="upload"/><br>
  18.       选择2文件:<input type="file" name="upload"/><br>
  19.       选择3文件:<input type="file" name="upload"/><br>
  20.      <input type="submit" value="上传"/> 
  21.     </form>
  22.   </body>
  23. </html>

 

3.strust.xml,采用fileUpload拦截器实现文件类型和大小验证

  1. <?xml version="1.0" encoding="GBK"?>
  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. <struts>
  6.     <constant name="struts.custom.i18n.resources" value="globalMessages"/>
  7.     <constant name="struts.i18n.encoding" value="utf-8"/>
  8.     <package name="lee" extends="json-default">
  9.         <global-exception-mappings>
  10.             <!-- 指Action抛出AuctionException异常时,转入名为exception的结果。 -->
  11.             <exception-mapping exception="com.hxz.excepton.NameException" result="exception"/>
  12.             <!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
  13.             <exception-mapping exception="java.lang.Exception" result="exception"/>
  14.         </global-exception-mappings>
  15.         <action name="readName" class="com.hxz.action.NameAction">
  16.             <result name="success">/testJstl.jsp</result>
  17.             <result name="error">/error.jsp</result>
  18.         </action>
  19.         <action name="getNameById" class="com.hxz.action.NameAction" method="getNameById">
  20.             <result name="success" type="json">/name.html</result>
  21.             <result name="error">/error.jsp</result>
  22.         </action>
  23.          <action name="updateName" class="com.hxz.action.NameAction" method="updateName">
  24.             <result name="success" >/name.html</result>    
  25.             <result name="error">/error.jsp</result>
  26.         </action>
  27.         <action name="upload" class="com.hxz.action.UploadAction">
  28.           <interceptor-ref name="fileUpload">
  29.             <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/pjpeg</param>
  30.             <param name="maximumSize">2000</param>
  31.           </interceptor-ref>
  32.           <interceptor-ref name="defaultStack"/>
  33.             <param name="savePath">/upload</param>
  34.             <result name="zhe">/upload/succ.jsp</result>
  35.             <result name="input">/upload/upload.jsp</result>
  36.         </action>
  37.            </package> 
  38. </struts>

4.成功页面succ.jsp

  1. <%@ page language="java"  pageEncoding="GBK"%>
  2. <%@ taglib prefix="s" uri="/struts-tags"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5.   <head>
  6.    
  7.     
  8.     <title>succ.jsp</title>
  9.     <meta http-equiv="pragma" content="no-cache">
  10.     <meta http-equiv="cache-control" content="no-cache">
  11.     <meta http-equiv="expires" content="0">    
  12.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  13.     <meta http-equiv="description" content="This is my page">
  14.   </head>
  15.   <body>
  16.     文件标题:<s:property value="+title"/><br>
  17.    <img  src="<s:property value="uploadFileName[0]"/>" />
  18.    <img  src="<s:property value="uploadFileName[1]"/>" />
  19.    <img  src="<s:property value="uploadFileName[2]"/>" />
  20.   </body>
  21. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值