1)Struts2单文件上传

action:类文件

package com.mlq.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import java.io.File;
import java.io.IOException; /**
* Struts2:单文件上传
*
* @author asus
*/
public class UploadFileAction extends ActionSupport { //临时文件
private File upload;
//文件类型(底层规定属性名称)
private String uoloadContextType;
//文件名称(底层规定属性名称)
private String uploadFileName;
//文件存放路径
private String savePath; @Override
public void validate() {
System.out.println("严重方法");
} //默认访问方法
@Override
public String execute() throws IOException {
//文件全路径
File destFile = new
File(ServletActionContext.getRequest().getRealPath(savePath) + "\\" + getUploadFileName());
String path = destFile.getPath();
System.out.println(path + "------");
FileUtils.copyFile(upload, destFile);
return "success";
} public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUoloadContextType() {
return uoloadContextType;
}
public void setUoloadContextType(String uoloadContextType) {
this.uoloadContextType = uoloadContextType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}

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="user" namespace="/user" extends="struts-default">
<!--全局配置信息-->
<global-results>
<result name="input">/upload.jsp</result>
</global-results>
<!--文件上传-->
<action name="upload" class="com.mlq.action.UploadFileAction">
<!--内置设定action类中的属性值-->
<param name="savePath">/upload</param>
<result>/upload.jsp</result>
<!--设定文件参数-->
<interceptor-ref name="fileUpload">
<param name="maximumSize">512000</param>
<param name="allowedType">image/jpg</param>
</interceptor-ref>
<!--默认的拦截器-->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>

upload.jsp:JSP页面

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2018/10/4
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>StrutsTow文件上传</title>
</head>
<body>
<div>
<fieldset>
<legend>Struts2文件上传</legend>
<form action="/user/upload" enctype="multipart/form-data" method="post">
选择文件:<input type="file" name="upload">
<input type="submit" value="提交">
</form>
</fieldset>
<br/><br/>
<fieldset>
<legend>上传的图片回显</legend>
<img width="200px" height="300px" src="/upload/<s:property value='uploadFileName'/>"/>
</fieldset>
<br/><br/>
<fieldset>
<legend>错误信息</legend>
<s:fielderror></s:fielderror>
</fieldset>
</div>
</body>
</html>

Web.xml:核心配置

    <!--核心控制器-->
<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> <welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>

2)Struts2多文件上传

action:类文件

package com.mlq.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import java.io.File;
import java.io.IOException; /**
* Struts2:单文件上传
*
* @author asus
*/
public class UploadsFileAction extends ActionSupport { //临时文件
private File[] upload;
//文件类型
private String[] uoloadContextType;
//文件名称
private String[] uploadFileName;
//文件存放路径
private String savePath; @Override
public void validate() {
System.out.println("严重方法");
} //默认访问方法
@Override
public String execute() throws IOException {
for (int i = 0; i < upload.length; i++) {
File temp=upload[i];
//文件全路径
File destFile = new
File(ServletActionContext.getRequest().getRealPath(savePath) + "\\" + uploadFileName[i]);
String path = destFile.getPath();
System.out.println(path + "------");
FileUtils.copyFile(temp, destFile);
}
return "success";
} public File[] getUpload() {
return upload;
} public void setUpload(File[] upload) {
this.upload = upload;
} public String[] getUoloadContextType() {
return uoloadContextType;
} public void setUoloadContextType(String[] uoloadContextType) {
this.uoloadContextType = uoloadContextType;
} public String[] getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getSavePath() {
return savePath;
} public void setSavePath(String savePath) {
this.savePath = savePath;
}
}

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="5097152"/>
<package name="user" namespace="/user" extends="struts-default">
<!--全局配置信息-->
<global-results>
<result name="input">/upload.jsp</result>
</global-results>
<!--文件上传-->
<action name="upload" class="com.mlq.action.UploadsFileAction">
<!--内置设定action类中的属性值-->
<param name="savePath">/upload</param>
<result>/upload.jsp</result>
<!--设定文件参数-->
<interceptor-ref name="fileUpload">
<param name="maximumSize">512000</param>
<param name="allowedType">image/jpg</param>
</interceptor-ref>
<!--默认的拦截器-->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>

upload.jsp:JSP页面

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2018/10/4
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>StrutsTow文件上传</title>
</head>
<body>
<div> <fieldset>
<legend>Struts2文件上传</legend>
<form action="/user/upload" enctype="multipart/form-data" method="post">
选择文件:<input type="file" name="upload">
<br/>
选择文件:<input type="file" name="upload">
<br/>
选择文件:<input type="file" name="upload">
<input type="submit" value="提交">
</form>
</fieldset>
<br/><br/>
<fieldset>
<legend>上传的图片回显</legend>
<s:iterator value="uploadFileName">
<img width="200px" height="300px" src="/upload/<s:property/>"/>
</s:iterator>
</fieldset>
<br/><br/>
<fieldset>
<legend>错误信息</legend>
<s:fielderror></s:fielderror>
</fieldset> </div>
</body>
</html>

web.xml:核心配置

<!--核心控制器-->
<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> <welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>

3)Struts2文件下载

action:类文件

package com.mlq.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream; /**
* Struts2:文件下载
*
* @author asus
*/
public class FileDownAction extends ActionSupport { /***
* 读取下载文件的目录
*/
private String inputPath;
/***
* 下载文件的文件名
*/
private String fileName;
/***
* 读取下载文件的输入流
*/
private InputStream inputStream; public String getInputPath() {
return inputPath;
} public void setInputPath(String inputPath) {
this.inputPath = inputPath;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public InputStream getInputStream() throws FileNotFoundException {
String realPath =
ServletActionContext.getServletContext().getRealPath(inputPath);
System.out.println(realPath+"\\"+fileName);
return new BufferedInputStream(new FileInputStream(realPath+"\\"+fileName));
} public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}

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="user" namespace="/user" extends="struts-default">
<!--文件上传-->
<action name="downLoad" class="com.mlq.action.FileDownAction">
<!--指定文件下载目录地址-->
<param name="inputPath">/upload</param>
<!--设置下载类型-->
<result name="success" type="stream">
<!--设置发送到浏览器的MIMe类型-->
<param name="contentType">application/octet-stream</param>
<!--设置输入流的名称-->
<param name="inputStream">inputStream</param>
<!--提示用户打开还是下载-->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<!--缓冲区的大小:没有严格要求可随意设置-->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2018/10/4
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>StrutsTow文件下载</title>
</head>
<body>
<div> <fieldset>
<legend>Struts2文件下载</legend>
<a href="/user/downLoad?fileName=t01a4036c8714c169fd.jpg">文件下载</a>
</fieldset> </div>
</body>
</html>

web.xml核心配置

<!--核心控制器-->
<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>

单文件上传Demo:    多文件上传Demo:    文件下载Demo:

 

分享知识-快乐自己:Struts2文件上传及文件下载的更多相关文章

  1. Struts2文件上传和文件下载

    一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="uplo ...

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

    一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="uplo ...

  3. struts2的文件上传和文件下载

    实现使用Struts2文件上传和文件下载: 注意点: (1)对应表单的file1和私有成员变量的名称必须一致 <input type="file" name="fi ...

  4. 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载

    摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...

  5. 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】

    一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...

  6. springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传

    总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...

  7. Struts2文件上传下载

    Struts2文件上传 Struts2提供 FileUpload拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容,fileUpload拦截器 默认在defau ...

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

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

  9. struts2文件上传大小限制问题小结

    一:首先看一下程序执行中出现的对应报错信息,如下所示: [WARN ] 2015-03-03 15:31:11 :Unable to parse request org.apache.commons. ...

随机推荐

  1. Vue 内容分发slot

    1.概述 内容分发:混合父组件的内容与子组件自己的模板. 2.单个插槽 当子组件模板只有一个没有属性的插槽时,父组件传入的整个内容片段将插入到插槽所在的 DOM 位置,并替换掉插槽标签本身. 最初在  ...

  2. Activity 事件以及如何得到新打开Activity关闭后返回的数据

    1: package com.example.activity_basic; 2:   3: import android.os.Bundle; 4: import android.app.Activ ...

  3. (七)jQuery中的DOM操作

    一.jQuery的DOM操作 (1)查找节点: 查找元素节点: 1. 获取指定的标签节点 $(“上级标签 标签:eq(“标签索引”) ;  如:var li = $("ul li:eq(2) ...

  4. Anaconda2

    Anaconda 是一个打包的python,一次把好多需要的包都安装好了.对于Python2.7把PyQt5都弄好了,不需要自己来编译! 看看这个 http://conda.pydata.org/do ...

  5. android studio 更新Gradle版本号方法

    在导入其它项目时,常常因为gradle版本号不一致而导致不能编译 解决方法: 第一步: 按提示点击让它下载.事实上目的并非要它下载.因为这样速度会非常慢.这样做仅仅是为了让它在本地创建相应的文件夹结构 ...

  6. [Linux] 网络

    如何在网络中标识一台计算机 IP 多个程序如何不冲突 通信端口 不同的计算机如何通信 协议 IP A类:0+7位网络号+24位主机号,可用网络2^7-2个,每个网络可容纳2^24-2个主机 B类:10 ...

  7. python 基础 2.6 for 循环 和if循环 中break

    python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...

  8. 【BZOJ2597】[Wc2007]剪刀石头布 最小费用流

    [BZOJ2597][Wc2007]剪刀石头布 Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之 ...

  9. 通用分页(Jquery版)

    1.简单定义下样式 <style type="text/css"> .fanye { color: blue; margin-right: 15px; text-dec ...

  10. 九度OJ 1035:找出直系亲属 (二叉树、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ...