所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

第一节:Struts2 文件上传

Struts2 文件上传基于Struts2 拦截器实现;

Struts2 文件上传使用的是fileupload 组件;

Form 配置enctype="multipart/form-data";(这样就是以二进制来上传的)

Struts2 获取上传文件:name (name 是文件表单的name)

Struts2 获取上传文件名:name+FileName;

Struts2 获取上传文件的类型:name+ContentType;

例子:

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="manager" extends="struts-default"> <action name="upload" class="com.wishwzp.action.FileUploadAction">
<result name="success">/success.jsp</result> </action> </package> </struts>

FileUploadAction.java

 package com.wishwzp.action;

 import java.io.File;

 import org.apache.commons.io.FileUtils;

 import com.opensymphony.xwork2.ActionSupport;

 public class FileUploadAction extends ActionSupport{

     /**
*
*/
private static final long serialVersionUID = 1L; // 文件(这里的文件名必须要和前端的文件:<input type="file" name="wishwzp"/><br/>name一样)
private File wishwzp; // 文件名(name+FileName)
private String wishwzpFileName; // 文件类型(name+ContentType)
private String wishwzpContentType; public File getwishwzp() {
return wishwzp;
} public void setwishwzp(File wishwzp) {
this.wishwzp = wishwzp;
} public String getwishwzpFileName() {
return wishwzpFileName;
} public void setwishwzpFileName(String wishwzpFileName) {
this.wishwzpFileName = wishwzpFileName;
} public String getwishwzpContentType() {
return wishwzpContentType;
} public void setwishwzpContentType(String wishwzpContentType) {
this.wishwzpContentType = wishwzpContentType;
} @Override
public String execute() throws Exception {
System.out.println("文件名:"+wishwzpFileName);
System.out.println("文件类型:"+wishwzpContentType);
//上传到C盘中
String filePath="C:/"+wishwzpFileName;
//保存文件添加到文件中
File saveFile=new File(filePath);
//将wishwzp文件拷到saveFile中,这样数据就到C盘中了
FileUtils.copyFile(wishwzp, saveFile);
return SUCCESS;
} }

fileupload.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 这里enctype="multipart/form-data就是将数据以二进制上传 -->
<form action="upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="wishwzp"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

success.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
文件上传成功!
</body>
</html>

第二节:配置文件的大小及类型

配置可以上传哪些文件的信息以及最大上传的大小

<param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>

<param name="maximumSize">81101</param>

例子:

struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="manager" extends="struts-default"> <action name="upload" class="com.wishwzp.action.FileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/fileupload.jsp</result> <interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
<param name="maximumSize">81101</param>
</interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>

前端页面文件错误表达式:

<s:fielderror></s:fielderror>

例子:

fileupload.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<!-- 这里enctype="multipart/form-data就是将数据以二进制上传 -->
<form action="upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="wishwzp"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

第三节:大文件上传

Struts2 文件上传大小默认是2M;

<constant name="struts.multipart.maxSize" value="20000000"></constant>

例子:

struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 上传大小配置 -->
<constant name="struts.multipart.maxSize" value="20000000"></constant> <package name="manager" extends="struts-default"> <action name="upload" class="com.wishwzp.action.FileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/fileupload.jsp</result> </action> </package> </struts>

第四节:多文件上传

strutsl.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="manager" extends="struts-default">
<action name="uploads" class="com.wishwzp.action.FilesUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/filesupload.jsp</result>
</action> </package> </struts>

FilesUploadAction.java

 package com.wishwzp.action;

 import java.io.File;

 import org.apache.commons.io.FileUtils;

 import com.opensymphony.xwork2.ActionSupport;

 public class FilesUploadAction extends ActionSupport{

     /**
*
*/
private static final long serialVersionUID = 1L; // 文件(这里的文件名必须要和前端的文件:<input type="file" name="wishwzp"/><br/>name一样)
private File[] wishwzp; // 文件名(name+FileName)
private String[] wishwzpFileName; // 文件类型(name+ContentType)
private String[] wishwzpContentType; public File[] getwishwzp() {
return wishwzp;
} public void setwishwzp(File[] wishwzp) {
this.wishwzp = wishwzp;
} public String[] getwishwzpFileName() {
return wishwzpFileName;
} public void setwishwzpFileName(String[] wishwzpFileName) {
this.wishwzpFileName = wishwzpFileName;
} public String[] getwishwzpContentType() {
return wishwzpContentType;
} public void setwishwzpContentType(String[] wishwzpContentType) {
this.wishwzpContentType = wishwzpContentType;
} @Override
public String execute() throws Exception {
for(int i=0;i<wishwzp.length;i++){
System.out.println("文件名:"+wishwzpFileName[i]);
System.out.println("文件类型:"+wishwzpContentType[i]);
//上传到C盘中
String filePath="C:/"+wishwzpFileName[i];
//保存文件添加到文件中
File saveFile=new File(filePath);
//将wishwzp文件拷到saveFile中,这样数据就到C盘中了
FileUtils.copyFile(wishwzp[i], saveFile);
}
return SUCCESS;
} }

filesupload.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form action="uploads" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="wishwzp"/><br/>
文件2:<input type="file" name="wishwzp"/><br/>
文件3:<input type="file" name="wishwzp"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

第五节:Struts2 文件下载

返回的是文件流

<param name="contentDisposition">attachment;filename=${fileName}</param>

InputStream getInputStream()

struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.multipart.maxSize" value="20000000"></constant> <package name="manager" extends="struts-default">
<action name="download" class="com.wishwzp.action.FileDownloadAction">
<result type="stream">
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action> </package> </struts>

FileDownloadAction.java

 package com.wishwzp.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class FileDownloadAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L; private String fileName; public String getFileName() throws Exception{
fileName=new String(fileName.getBytes(),"ISO8859-1");
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public InputStream getInputStream()throws Exception{
File file=new File("C:/美女1.jpg");
this.fileName="美女1号";
return new FileInputStream(file);
} }

filedownload.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="download">文件下载</a>
</body>
</html>

(八)Struts2 文件上传和下载的更多相关文章

  1. Struts2文件上传和下载(原理)

    转自:http://zhou568xiao.iteye.com/blog/220732 1.    文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1)     ...

  2. 十六、Struts2文件上传与下载

    文件上传与下载 1.文件上传前提:<form action="${pageContext.request.contextPath}/*" method="post& ...

  3. 【SSH2(实用文章)】--Struts2文件上传和下载的例子

    回想一下,再上一篇文章Struts2实现机制,该步骤做一步一步来解决,这种决心不仅要理清再次Struts2用法.映射机制及其在深入分析.最后一个例子来介绍Struts2一种用法,这里将做一个有关文件上 ...

  4. 学习Struts--Chap07:Struts2文件上传和下载

    1.struts2文件上传 1.1.struts2文件上传的基本概述 在开发web应用的时候,我们一般会为用户提供文件上传的功能,比如用户上传一张图像作为头像等.为了能上传文件,我们必须将表单的met ...

  5. struts2 文件上传和下载,以及部分源代码解析

    struts2 文件上传 和部分源代码解析,以及一般上传原理 (1) 单文件上传 一.简单介绍 Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form ...

  6. struts2学习(13)struts2文件上传和下载(1)

    一.Struts2文件上传: 二.配置文件的大小以及允许上传的文件类型: 三.大文件上传: 如果不配置上传文件的大小,struts2默认允许上传文件最大为2M: 2097152Byte:   例子实现 ...

  7. Struts2文件上传与下载

    一,页面 index.html 在页面中最重要的就是这个文件上传用的 form 表单,注意这里一定要把 form 的encyType属性明确标定为“multipart/form-data”,只有这样. ...

  8. struts2文件上传和下载

    1. struts系统中的拦截器介绍 过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...

  9. 笔记:Struts2 文件上传和下载

    为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器. 上传 ...

随机推荐

  1. Unity给力插件之LittleLocalization

    从网上随便下载了一个多语言的插件,原理很简单,自己就可以写一个.不过本身插件做的算比较全面,拿来就能用,所以精简了一下,然后重新打了个包. 为了快速使用插件,在此把步骤列出来. 1.创建空物体,添加L ...

  2. PHP检查表单提交是否来自于本站(验证HTTP_REFERER等)

    方法一: 你可以把处理提交数据的代码写到一个单独的文件里,比如form.php.      <?php      if   (defined(’INSIDE’))   {//判断是否有定义INS ...

  3. Mysql导出导入乱码问题解决

    MySQL从4.1版本开始才提出字符集的概念,所以对于MySQL4.0及其以下的版本,他们的字符集都是Latin1的,所以有时候需要对mysql的字符集进行一下转换,MySQL版本的升级.降级,特别是 ...

  4. AVL树的左旋右旋理解 (转)

    AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...

  5. hdoj 1862 EXCEL排序

    EXCEL排序 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 字符串hash函数

    本文搜集了一些字符串的常用hash函数. 范例1:判断两个单词是否含有相同的字母,此时我们可以用hash做.例如,“aaabb”与"aabb"含有相同的单词.(参考:http:// ...

  7. 一次服务器CPU占用率高的定位分析

    现象: 当前项目启动一段时间,有一个服务导致CPU使用率持续超过30% 环境:Windows 7,  CPU: 8核, 内存: 8g内存 定位过程: 启动项目,查看Java进程ID 查看Event P ...

  8. mysql 插入默认值的问题 sql-mode

    刚好碰到如果不给默认值mysql数据就插入不成功的问题,后来百度了很多,试了下结果 把my.ini里面的[mysqld]的sql-mode 换成下面的一行,如果没有则添加  sql-mode=&quo ...

  9. engine的工具中实现Snapping(捕捉)

    在Engine的工具(ITool)里: OnClick事件处理函数中: 首先需要获取一个图层,作为Snapping的参照, IFeatureLayer targetLayer 然后声明一个IMoveP ...

  10. <input type="radio" >与<input type="checkbox">值得获取

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...