一、单文件的上传 

表单的enctype属性:

application/x-www-form-urlencoded:默认的编码方式。

multipart/form-data:会以二进制的方式处理表单数据,会将文件域指定文件的内容也封装到请求参数里。

text/plain:当action属性是mailto:URL的形式方便。

常用的上传框架是Common-FileUpload和COS,默认是Common-FileUpload,一旦框架获得了文件域对应的文件内容,我们就可以通过IO流将文件内容写入服务器的任意位置,从而完成文件上传。

可以通过struts.properties配置文件中,修改默认值:struts.multipart.parser=jakarta

1、处理上传文件的jsp页面

upload.jsp

  1: <%@ page language="java" contentType="text/html; charset=UTF-8"
  2:     pageEncoding="UTF-8"%>
  3: <%@ page import="java.util.*" %>
  4: <%@ taglib prefix="s" uri="/struts-tags" %>
  5: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6: <html>
  7: <%
  8: String path = request.getContextPath();
  9: String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 10: %>
 11: <head>
 12:     <base href="<%=basePath%>">
 13:     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 14:     <title>Struts2文件上传</title>
 15: </head>
 16: <body>
 17:       <h1>Struts 2完成上传</h1>
 18:       <form action="upload.action" method="post" enctype="multipart/form-data">
 19:         <div style="color:red"><s:fielderror/> </div>
 20:         <table>
 21:             <tr>
 22:                 <td>用户名:</td>
 23:                 <td><input type="text" name="username" ></td>
 24:             </tr>
 25:             <tr>
 26:                 <td>上传文件:</td>
 27:                 <td><input type="file" name="myFile"></td>
 28:             </tr>
 29:             <tr>
 30:                 <td><input type="submit" value="上传"></td>
 31:                 <td><input type="reset"></td>
 32:             </tr>
 33:         </table>
 34:       </form>
 35:   </center>
 36: </body>
 37: </html>

2、UploadAction.java

  1: package cc.openhome;
  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.opensymphony.xwork2.ActionSupport;
  9: public class UploadAction extends ActionSupport {
 10:   // username属性用来封装用户名
 11:   private String username;
 12:
 13:   // myFile属性用来封装上传的文件
 14:   private File myFile;
 15:
 16:   // myFileContentType属性用来封装上传文件的类型
 17:   private String myFileContentType;
 18:
 19:   // myFileFileName属性用来封装上传文件的文件名
 20:   private String myFileFileName;
 21:
 22:
 23:   //获得username值
 24:   public String getUsername() {
 25:     return username;
 26:   }
 27:
 28:   //设置username值
 29:   public void setUsername(String username) {
 30:     this.username = username;
 31:   }
 32:
 33:   //获得myFile值
 34:   public File getMyFile() {
 35:     return myFile;
 36:   }
 37:
 38:   //设置myFile值
 39:   public void setMyFile(File myFile) {
 40:     this.myFile = myFile;
 41:   }
 42:
 43:   //获得myFileContentType值
 44:   public String getMyFileContentType() {
 45:     return myFileContentType;
 46:   }
 47:
 48:   //设置myFileContentType值
 49:   public void setMyFileContentType(String myFileContentType) {
 50:     this.myFileContentType = myFileContentType;
 51:   }
 52:
 53:   //获得myFileFileName值
 54:   public String getMyFileFileName() {
 55:     return myFileFileName;
 56:   }
 57:
 58:   //设置myFileFileName值
 59:   public void setMyFileFileName(String myFileFileName) {
 60:     this.myFileFileName = myFileFileName;
 61:   }
 62:
 63:   public String execute() throws Exception {
 64:
 65:     //基于myFile创建一个文件输入流
 66:     InputStream is = new FileInputStream(myFile);
 67:
 68:     // 设置上传文件目录
 69:     String uploadPath = ServletActionContext.getServletContext()
 70:         .getRealPath("/upload");
 71:
 72:     // 设置目标文件
 73:     File toFile = new File(uploadPath, this.getMyFileFileName());
 74:
 75:     // 创建一个输出流
 76:     OutputStream os = new FileOutputStream(toFile);
 77:
 78:     //设置缓存
 79:     byte[] buffer = new byte[1024*1024];
 80:
 81:     int length = 0;
 82:
 83:     //读取myFile文件输出到toFile文件中
 84:     while ((length = is.read(buffer)) > 0) {
 85:       os.write(buffer, 0, length);
 86:     }
 87:     //关闭输入流
 88:     is.close();
 89:     //关闭输出流
 90:     os.close();
 91:     return SUCCESS;
 92:   }
 93: }
 94:
 95: 

    3、配置struts.xml

  1: <?xml version="1.0" encoding="UTF-8" ?>
  2: <!DOCTYPE struts PUBLIC
  3:   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4:   "http://struts.apache.org/dtds/struts-2.3.dtd">
  5:
  6: <struts>
  7:   <constant name="struts.custom.i18n.resources" value="messageResource"></constant>
  8:   <package name="openhome" extends="struts-default">
  9:     <action name="upload" class="cc.openhome.UploadAction">
 10:       <interceptor-ref name="fileUpload">
 11:         <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
 12:         <param name="maximumSize">2000</param>
 13:       </interceptor-ref>
 14:       <interceptor-ref name="defaultStack" />
 15:       <result name="success">/WEB-INF/page/succ.jsp</result>
 16:       <result name="input">/upload.jsp</result>
 17:     </action>
 18:   </package>
 19:     <!-- Add packages here -->
 20: </struts>
 21:   

    4、succ.jsp

  1: <%@ page language="java" contentType="text/html; charset=UTF-8"
  2:     pageEncoding="UTF-8"%>
  3: <%@ taglib prefix="s" uri="/struts-tags" %>
  4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5: <html>
  6: <head>
  7:     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8:     <title>Insert title here</title>
  9: </head>
 10: <body>
 11:     上传成功!<br>
 12:     上传用户:${username}<br>
 13:     文件标题:${ myFileFileName }<br>
 14:     文件类型:${ myFileContentType }<br>
 15:     文件为:<br><img src="<s:property value="'upload/'+myFileFileName"/>"/>
 16: </body>
 17: </html>

     5、运行结果

          此时,上传文件必须满足:大小小于2000kb,而且是图片文件。

     

二、多文件的上传

1、uploadmany.jsp

  1: <%@ page language="java" contentType="text/html; charset=UTF-8"
  2:     pageEncoding="UTF-8"%>
  3: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4: <html>
  5: <head>
  6:   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7:   <title>数组多文件上传</title>
  8: </head>
  9: <body>
 10:   <form action="upload.action" method="post" enctype="multipart/form-data">
 11:     上传用户:<input type="text" name="username" /><br>
 12:     选择第一个文件:<input type="file" name="myFile" /><br>
 13:     选择第二个文件:<input type="file" name="myFile" /><br>
 14:     选择第三个文件:<input type="file" name="myFile" /><br>
 15:     <input type="submit" value="上传">
 16:         <input type="reset">
 17:   </form>
 18: </body>
 19: </html>

2、UploadAction.jsp

  1: package cc.openhome;
  2:
  3: import java.io.File;
  4: import java.io.FileInputStream;
  5: import java.io.FileOutputStream;
  6: import java.io.InputStream;
  7: import java.io.OutputStream;
  8: import java.util.List;
  9: import org.apache.struts2.ServletActionContext;
 10: import com.opensymphony.xwork2.ActionSupport;
 11: public class UploadAction extends ActionSupport {
 12:   // username属性用来封装用户名
 13:   private String username;
 14:
 15:   // myFile属性用来封装上传的文件
 16:   //private File[] myFile;
 17:   private List<File> myFile;
 18:
 19:   // myFileContentType属性用来封装上传文件的类型
 20:   private List<String> myFileContentType;
 21:
 22:   // myFileFileName属性用来封装上传文件的文件名
 23:   private List<String> myFileFileName;
 24:
 25:   //定义所有Action允许上传的类型
 26:   private String allowTypes;
 27:
 28:   public String getAllowTypes() {
 29:     return allowTypes;
 30:   }
 31:
 32:   public void setAllowTypes(String allowTypes) {
 33:     this.allowTypes = allowTypes;
 34:   }
 35:
 36:   //获得username值
 37:   public String getUsername() {
 38:     return username;
 39:   }
 40:
 41:   //设置username值
 42:   public void setUsername(String username) {
 43:     this.username = username;
 44:   }
 45:
 46:   //获得myFile值
 47:   public List<File> getMyFile() {
 48:     return myFile;
 49:   }
 50:
 51:   //设置myFile值
 52:   public void setMyFile(List<File> myFile) {
 53:     this.myFile = myFile;
 54:   }
 55:
 56:   //获得myFileContentType值
 57:   public List<String> getMyFileContentType() {
 58:     return myFileContentType;
 59:   }
 60:
 61:   //设置myFileContentType值
 62:   public void setMyFileContentType(List<String> myFileContentType) {
 63:     this.myFileContentType = myFileContentType;
 64:   }
 65:
 66:   //获得myFileFileName值
 67:   public List<String> getMyFileFileName() {
 68:     return myFileFileName;
 69:   }
 70:
 71:   //设置myFileFileName值
 72:   public void setMyFileFileName(List<String> myFileFileName) {
 73:     this.myFileFileName = myFileFileName;
 74:   }
 75:   public String execute() throws Exception {
 76:     List<File> files=getMyFile();
 77:     for(int i=0;i<files.size();i++){
 78:       //基于myFile创建一个文件输入流
 79:       InputStream is = new FileInputStream(files.get(i));
 80:
 81:       // 设置上传文件目录
 82:       String uploadPath = ServletActionContext.getServletContext()
 83:           .getRealPath("/upload");
 84:       // 设置目标文件
 85:       //File[] toFile = new File(uploadPath, this.getMyFileFileName());
 86:       File toFile = new File(uploadPath, this.getMyFileFileName().get(i));
 87:       // 创建一个输出流
 88:       OutputStream os = new FileOutputStream(toFile);
 89:
 90:       //设置缓存
 91:       byte[] buffer = new byte[1024*1024];
 92:
 93:       int length = 0;
 94:
 95:       //读取myFile文件输出到toFile文件中
 96:       while ((length = is.read(buffer)) > 0) {
 97:         os.write(buffer, 0, length);
 98:       }
 99:       //关闭输入流
100:       is.close();
101:
102:       //关闭输出流
103:       os.close();
104:     }
105:     return SUCCESS;
106:   }
107: }
108:
109:
110: 

3、struts.xml

  1: <?xml version="1.0" encoding="UTF-8" ?>
  2: <!DOCTYPE struts PUBLIC
  3:   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4:   "http://struts.apache.org/dtds/struts-2.3.dtd">
  5:
  6: <struts>
  7:   <package name="sturts"  extends="struts-default">
  8:       <action name="upload" class="cc.openhome.UploadAction">
  9:           <interceptor-ref name="fileUpload">
 10:             <!-- 用于规定只能上传的文件类型 -->
 11:         <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
 12:         <param name="maximumSize">20000000</param>
 13:       </interceptor-ref>
 14:       <interceptor-ref name="defaultStack" />
 15:             <result name="success">/succ.jsp</result>
 16:             <result name="input">/upload.jsp</result>
 17:         </action>
 18:   </package>
 19: </struts>

4、succ.jsp

  1: <%@ page language="java" contentType="text/html; charset=UTF-8"
  2:     pageEncoding="UTF-8"%>
  3: <%@ taglib prefix="s" uri="/struts-tags" %>
  4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5: <html>
  6:   <head>
  7:     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8:     <title>图片显示</title>
  9:   </head>
 10:   <body>
 11:     <h2>上传成功!</h2>
 12:     上传用户:${ username }<hr><br>
 13:     第一个文件名:${ myFileFileName[0] }<br>
 14:     第一个文件类型:${ myFileContentType[0] }<br>
 15:     第一个文件为:<br><img src="<s:property value="'upload/'+myFileFileName[0]"/>"/><hr><br>
 16:     第二个文件名:${ myFileFileName[1] }<br>
 17:     第二个文件类型:${ myFileContentType[1] }<br>
 18:     第二个文件为:<br><img src="<s:property value="'upload/'+myFileFileName[1]"/>"/><hr><br>
 19:     第三个文件名:${ myFileFileName[2] }<br>
 20:     第三个文件类型:${ myFileContentType[2] }<br>
 21:     第三个文件为:<br><img src="<s:property value="'upload/'+myFileFileName[2]"/>"/><hr><br>
 22:   </body>
 23: </html>

5、运行结果

           

Struts2文件的上传的更多相关文章

  1. struts2 文件的上传下载 表单的重复提交 自定义拦截器

    文件上传中表单的准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设 ...

  2. Struts2 - 文件的上传和下载

    1.  前言 这个章节是Struts2框架应用最广泛的三个版块(上传下载.国际化.校验输入)之一,所以这一版块的学习还蛮重要的. 2.  具体内容 回到顶部 2.1Struts2文件上传 2.1.1单 ...

  3. struts2文件异步上传

    代码结构 用到的js文件: 用到的jar包: jar和js库文件下载地址:下载地址 首先我们写FileAction类 package com.ajaxfile.action; import java. ...

  4. Struts2 文件的上传与下载

    1. Struts2的文件上传需要Apache的commons-io-Version.jar和commons-fileupload-Version.jar两个jar包.2. 页面中的<s:fil ...

  5. Struts2文件的上传和下载实现

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  6. [原创]java WEB学习笔记72:Struts2 学习之路-- 文件的上传下载,及上传下载相关问题

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. 关于在Struts2框架下实现文件的上传功能

    struts2的配置过程 (1)在项目中加入jar包 (2)web.xml中filter(过滤器)的配置 <?xml version="1.0" encoding=" ...

  8. 使用Struts2实现文件的上传和下载

    (一)单个文件的上传步骤: 1.拷贝jar包:commons-fileupload.jar,  commons-io.jar 下载链接(文件上传.rar):http://www.cnblogs.com ...

  9. Struts2基础学习(六)—文件的上传和下载

    一.文件的上传 1.单个文件上传      Struts2使用拦截器完成了文件的上传,而且底层使用的也是FileUpload开源组件. 客户端注意事项: (1)method="post&qu ...

随机推荐

  1. android脚步---不同界面之间切换

    对于一个app,可能需要多个界面,使用Button或者其他控件在不同界面之间切换,那么如何做到呢 首先必须明确,一般一个activity.java文件一般只对应一个界面即一个layout.xml文件, ...

  2. IE6里面display:inlineblock使得块元素成行排列,没用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. hrbustoj 2013 Play Game 2(博弈)

    注释在代码里 /* 1.若输入2 ~ 9 ,因为Stan 是先手,所以Stan 必胜 2.若输入10~18 ,因为Ollie 是后手,不管第一次Stan 乘的是什么,Stan肯定在 2 ~ 9 之间, ...

  4. Swift 项目中常用的第三方框架

    Swift 项目中可能用到的第三方框架 字数1004 阅读4091 评论17 喜欢93 这里记录下swift开发中可能用的框架 , 最近浏览了不少,积累在这里,以后用的时候方便查阅.顺便推荐给大家! ...

  5. jsp设置footer底部内容

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. do from a specific ip

    ping -S 192.168.240.1 sohu.com telnet -b 192.168.240.1 sohu.com 80

  7. 水熊虫 - Nature Communication

    想发好文章?先看好文献! 生物信息分析类的文章都有着比较明显的套路,如果你深刻的掌握了这些套路,相信有一天你也能发Nature(子刊). Extremotolerant tardigrade geno ...

  8. matlab字符串操作

    字符串转换函数 abs        字符串到ASCII转换dec2hex        十进制数到十六进制字符串转换fprintf        把格式化的文本写到文件中或显示屏上hex2dec   ...

  9. 多机并行计算框架 和CoolHash数据库(可用于多机机器学习)

    http://my.oschina.net/fourinone/blog/289122 http://www.oschina.net/p/fourinone

  10. UWSGITOP-----监控uwsgi 性能

    启动 uwsgi -x etc/bfdds_cookiemapping_conf.xml --stats /tmp/stats.socket 查看 uwsgitop /tmp/stats.socket ...