struts2实现文件上传和下载
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来比较繁琐,而且不灵活,在学习了struts2后,struts2为文件上传下载提供了更好的实现机制,在这里我分别就单文件上传和多文件上传的源代码进行一下讲解,这里需要导入文件下载上传的两个jar文件,一个是commons-fileupload-1.2.2.jar,另一个是commons-io-2.0.1.jar
struts2单文件上传:
首先是一个jsp文件上传页面,这个比较简单,就是一个表单,里面有个文件上传框
- <!--在进行文件上传时,表单提交方式一定要是post的方式,因为文件上传时二进制文件可能会很大,还有就是enctype属性,这个属性一定要写成multipart/form-data,
- 不然就会以二进制文本上传到服务器端-->
- <s:form action="uploadOne" enctype="multipart/form-data" method="POST">
- <s:file name="upload" label="选择文件"/><br>
- <s:submit name="submit" value="上传文件"/>
- </s:form>
接下来是uploadOne部分代码,因为struts2对上传和下载都提供了很好的实习机制,所以在action这段我们只需要写很少的代码就行:
- public class uploadOne extends ActionSupport {
- //获取文件
- private File upload;
- //获取文件的类型
- private String uploadContentType;
- //上传文件的名称
- private String uploadFileName;
- //获取文件上传的路径
- private String path;
- @Override
- public String execute() throws Exception {
- byte[] bytes=new byte[];
- //读取文件
- FileInputStream fis=new FileInputStream(getUpload());
- //写入文件,并设置保存目录的路径
- FileOutputStream fos=new FileOutputStream(getPath()+"\\"+getUploadFileName());
- int length=fis.read(bytes);
- while (length>){
- fos.write(bytes,,length);
- length=fis.read(bytes);
- }
- fos.close();
- fos.flush();
- fis.close();
- return SUCCESS;
- }
- //获取上传文件的保存路径
- //通过读取存放目录获得保存路径
- public String getPath() {
- return ServletActionContext.getServletContext().getRealPath(path);
- }
- public void setPath(String path) {
- this.path = path;
- }
- public File getUpload() {
- return upload;
- }
- public void setUpload(File upload) {
- this.upload = upload;
- }
- public String getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(String uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- public String getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(String uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
- }
首先我们要清楚一点,这里的upload并不是真正指代jsp上传过来的文件,当文件上传过来时,struts2首先会寻找struts.multipart.saveDir(这个是在default.properties里面有)这个name所指定的存放位置,我们可以新建一个struts.properties属性文件来指定这个临时文件存放位置,如果没有指定,那么文件会存放在tomcat的apache-tomcat-7.0.29\work\Catalina\localhost\目录下,然后我们可以指定文件上传后的存放位置,通过输出流将其写到流里面就行了,这时我们就可以在文件夹里看到我们上传的文件了。
struts.xml配置
- <action name="uploadOne" class="cn.happy.action.uploadOne">
- <!--通过param参数设置保存目录的路径-->
- <param name="path">/upload</param>
- <result name="success">oneUpload/two.jsp</result>
- </action>
文件上传后我们还需要将其下载下来,其实struts2的文件下载原理很简单,就是定义一个输入流,然后将文件写到输入流里面就行,关键配置还是在struts.xml这个配置文件里配置:
FileDownloadAction代码如下:
- //读取下载文件的目录
- private String inputPath;
- //下载文件的文件名
- private String fileName;
- //读取下载文件的输入流
- private InputStream inputStream;
- //下载文件的类型
- private String contntType;
- //创建InputStream输入流
- public InputStream getInputStream() throws FileNotFoundException {
- String path= ServletActionContext.getServletContext().getRealPath(inputPath);
- return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
- }
- public void setInputStream(InputStream inputStream) {
- this.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;
- }
struts.xml配置
- <action name="first" class="cn.happy.action.FileAction">
- <param name="inputPath">/upload</param>
- <result name="success" type="stream">
- <!-- 指定下载文件的内容类型,text/plain是默认类型 -->
- <param name="contntType">application/octet-stream</param>
- <!-- inputName默认值是inputStream,如果action中用于读取
- 下载文件内容的属性名是inputStream,那么可以省略这个参数 -->
- <param name="inputName">inputStream</param>
- <!--动态获取文件名,从Action中的取得filename-->
- <param name="contentDisposition">
- attachment;filename="${fileName}"
- </param>
- <param name="bufferSize"></param>
- </result>
struts.xml配置文件有几个地方我们要注意,首先是result的类型,以前我们定义一个action,result那里我们基本上都不写type属性,因为其默认是请求转发(dispatcher)的方式,除了这个属性一般还有redirect(重定向)等这些值,在这里因为我们用的是文件下载,所以type一定要定义成stream类型,告诉action这是文件下载的result,result元素里面一般还有param子元素,这个是用来设定文件下载时的参数,inputName这个属性就是得到action中的文件输入流,名字一定要和action中的输入流属性名字相同,然后就是contentDisposition属性,这个属性一般用来指定我们希望通过怎么样的方式来处理下载的文件,如果值是attachment,则会弹出一个下载框,让用户选择是否下载,如果不设定这个值,那么浏览器会首先查看自己能否打开下载的文件,如果能,就会直接打开所下载的文件,(这当然不是我们所需要的),另外一个值就是filename这个就是文件在下载时所提示的文件下载名字。
contentDispoistion参数由两部分组成,前面的部分表示处理文件的形式,如attachement表示在下载是弹出对话框,提示用户保存或者直接打开该文件;而后一部分表示下载文件的文件名称。两部分之间以;进行分隔。
在配置完这些信息后,我们就能过实现文件的下载功能了。
下载文件jsp页面
- <a href="first?fileName=ehcache.xml">点击此处下载文件</a>
struts2多文件上传:
其实多文件上传和单文件上传原理一样,单文件上传过去的是单一的File,多文件上传过去的就是一个List<File>集合或者是一个File[]数组,首先我们来看一下前端jsp部分的代码,
- <s:form action="more" enctype="multipart/form-data" method="POST">
- <s:file name="upload" label="选择文件"/><br>
- <s:file name="upload" label="选择文件"/><br>
- <s:submit name="submit" value="上传文件"/>
- </s:form>
file的名字必须都命名成upload才行,然后处理多文件上传的action代码如下:
- public class MoreUploadAction extends ActionSupport{
- //获取文件
- private List<File> upload;
- //获取文件的类型
- private List<String> uploadContentType;
- //上传文件的名称
- private List<String> uploadFileName;
- //获取文件上传的路径
- private String path;
- @Override
- public String execute() throws Exception {
- byte[] bytes=new byte[];
- int index=;
- for (File item:upload) {
- FileInputStream inputStream=new FileInputStream(item);
- FileOutputStream output=new FileOutputStream(getPath()+"\\"+getUploadFileName().get(index++));
- int length=inputStream.read(bytes);
- while (length>){
- output.write(bytes,,length);
- length=inputStream.read(bytes);
- }
- output.flush();
- output.close();
- inputStream.close();
- }
- return SUCCESS;
- }
- //获取上传文件的保存路径
- //通过读取存放目录获得保存路径
- public String getPath() {
- return ServletActionContext.getServletContext().getRealPath(path);
- }
- public void setPath(String path) {
- this.path = path;
- }
- public List<File> getUpload() {
- return upload;
- }
- public void setUpload(List<File> upload) {
- this.upload = upload;
- }
- public List<String> getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(List<String> uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- public List<String> getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(List<String> uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
struts.xml配置
- <!--多文件上传-->
- <action name="more" class="cn.happy.action.MoreUploadAction">
- <!--通过param参数设置保存目录的路径-->
- <param name="path">/upload</param>
- <result name="success">moreUpload/two.jsp</result>
- </action>
这样同样将其写到一个输出流里面,这样我们就可以在文件夹里看到上传的多个文件了
接下来的文件下载就和刚才的文件下载一模一样,struts.xml也是一样的,这里就不再重复了
总结:总的来说,struts2提供的文件上传下载机制简化了我们很多代码,我们可以在以后的项目中使用该机制,同样我们也可以使用FileUpload组件来进行文件的上传,这个都是因个人爱好决定!
struts2实现文件上传和下载的更多相关文章
- Struts2的文件上传与下载
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- 【Struts2】文件上传与下载
一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...
- (十五)struts2的文件上传和下载
文件上传的原理 我们以前学习上传的时候知道需要将表单的enctype属性设置为multipart/form-data. 表单的enctype属性指定的是表单数据的编码方式,有三个值: -applica ...
- Struts2之文件上传与下载
时间:2017-1-11 15:47 --使用commons-fileupload组件上传1.客户端 * method="post" * <input t ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- Struts2入门(七)——Struts2的文件上传和下载
一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...
- Struts2 实现文件上传和下载
实现上传功能 Struts2实际上是使用的commons fileupload 组件,所以记得导包哦. 1.首先你应该有一个上传页面 <!-- file的name属性与action中的File类 ...
- 7、Struts2实现文件上传和下载
一.实现单个文件上传 1.创建如下web项目结构 2.在src下的com.action包下创建UploadAction.java package com.action; import java.io. ...
- struts2(六) 文件上传和下载
前面对文件下载提过一点点,这里正好要讲文件上传,就放在一起在说一遍. --WH 一.单文件上传 在没学struts2之前,我们要写文件上传,非常麻烦,需要手动一步步去获取表单中的各种属性,然后在进行相 ...
随机推荐
- python yaml用法详解
YAML是一种直观的能够被电脑识别的的数据序列化格式,容易被人类阅读,并且容易和脚本语言交互.YAML类似于XML,但是语法比XML简单得多,对于转化成数组或可以hash的数据时是很简单有效的. Py ...
- Socket编程 之使用fsockopen()函数
fsockopen函数:初始化一个套接字连接到指定主机(hostname) get方式: client.php <?php //创建连接 $fp=fsockopen('localhost',80 ...
- js 空语句
不写就行了 ){}
- vw、vh、vmin、vmax、em、rem的使用详解
转载自:https://blog.csdn.net/ZNYSYS520/article/details/76053961 1,vw.vh.vmin.vmax 的含义 (1)vw.vh.vmin.vma ...
- python kline
# -*- coding: utf-8 -*- # Qt相关和十字光标 from qtpy.QtGui import * from qtpy.QtCore import * from qtpy imp ...
- printf打印输出
int PrintVal = 9; /*按整型输出,默认右对齐*/ printf("%d\n",PrintVal); /*按整型输出,补齐4位的宽度,补齐位为空格 ...
- MySQL的随机数函数rand()的使用技巧
咱们学php的都知道,随机函数rand或mt_rand,可以传入一个参数,产生0到参数之间的随机整数,也可以传入两个参数,产生这两个参数之间的随机整数. 而在mysql里,随机数函数rand不能传参, ...
- 博客搬家一下到CSDN
博客搬家一下到CSDN:http://blog.csdn.net/weixin_33409246
- eclipse运行项目,tomcat报错:Exception in thread :http-bio-8080-exec-4
eclipse运行项目,tomcat报错:Exception in thread :http-bio-8080-exec-4 转自 https://www.cnblogs.com/yby-blogs/ ...
- Python的类的下划线命名的区别
首先,单下划线开头,这个常被用于模块中,在一个模块中以单下划线开头的变量和函数被默认当做内部函数,如果使用from module import *导入时,这部分变量和函数不会被导入.注意,如果使 ...