在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来比较繁琐,而且不灵活,在学习了struts2后,struts2为文件上传下载提供了更好的实现机制,在这里我分别就单文件上传和多文件上传的源代码进行一下讲解,这里需要导入文件下载上传的两个jar文件,一个是commons-fileupload-1.2.2.jar,另一个是commons-io-2.0.1.jar

struts2单文件上传:

首先是一个jsp文件上传页面,这个比较简单,就是一个表单,里面有个文件上传框

  1. <!--在进行文件上传时,表单提交方式一定要是post的方式,因为文件上传时二进制文件可能会很大,还有就是enctype属性,这个属性一定要写成multipart/form-data,
  2.   不然就会以二进制文本上传到服务器端-->
  3. <s:form action="uploadOne" enctype="multipart/form-data" method="POST">
  4. <s:file name="upload" label="选择文件"/><br>
  5. <s:submit name="submit" value="上传文件"/>
  6. </s:form>

接下来是uploadOne部分代码,因为struts2对上传和下载都提供了很好的实习机制,所以在action这段我们只需要写很少的代码就行:

  1. public class uploadOne extends ActionSupport {
  2. //获取文件
  3. private File upload;
  4. //获取文件的类型
  5. private String uploadContentType;
  6. //上传文件的名称
  7. private String uploadFileName;
  8. //获取文件上传的路径
  9. private String path;
  10. @Override
  11. public String execute() throws Exception {
  12. byte[] bytes=new byte[];
  13. //读取文件
  14. FileInputStream fis=new FileInputStream(getUpload());
  15. //写入文件,并设置保存目录的路径
  16. FileOutputStream fos=new FileOutputStream(getPath()+"\\"+getUploadFileName());
  17. int length=fis.read(bytes);
  18. while (length>){
  19. fos.write(bytes,,length);
  20. length=fis.read(bytes);
  21. }
  22. fos.close();
  23. fos.flush();
  24. fis.close();
  25. return SUCCESS;
  26. }
  27. //获取上传文件的保存路径
  28. //通过读取存放目录获得保存路径
  29. public String getPath() {
  30. return ServletActionContext.getServletContext().getRealPath(path);
  31. }
  32.  
  33. public void setPath(String path) {
  34. this.path = path;
  35. }
  36. public File getUpload() {
  37. return upload;
  38. }
  39.  
  40. public void setUpload(File upload) {
  41. this.upload = upload;
  42. }
  43.  
  44. public String getUploadContentType() {
  45. return uploadContentType;
  46. }
  47.  
  48. public void setUploadContentType(String uploadContentType) {
  49. this.uploadContentType = uploadContentType;
  50. }
  51.  
  52. public String getUploadFileName() {
  53. return uploadFileName;
  54. }
  55.  
  56. public void setUploadFileName(String uploadFileName) {
  57. this.uploadFileName = uploadFileName;
  58. }
  59.  
  60. }

首先我们要清楚一点,这里的upload并不是真正指代jsp上传过来的文件,当文件上传过来时,struts2首先会寻找struts.multipart.saveDir(这个是在default.properties里面有)这个name所指定的存放位置,我们可以新建一个struts.properties属性文件来指定这个临时文件存放位置,如果没有指定,那么文件会存放在tomcat的apache-tomcat-7.0.29\work\Catalina\localhost\目录下,然后我们可以指定文件上传后的存放位置,通过输出流将其写到流里面就行了,这时我们就可以在文件夹里看到我们上传的文件了。

struts.xml配置

  1. <action name="uploadOne" class="cn.happy.action.uploadOne">
  2. <!--通过param参数设置保存目录的路径-->
  3. <param name="path">/upload</param>
  4. <result name="success">oneUpload/two.jsp</result>
  5. </action>

文件上传后我们还需要将其下载下来,其实struts2的文件下载原理很简单,就是定义一个输入流,然后将文件写到输入流里面就行,关键配置还是在struts.xml这个配置文件里配置:

FileDownloadAction代码如下:

  1. //读取下载文件的目录
  2. private String inputPath;
  3. //下载文件的文件名
  4. private String fileName;
  5. //读取下载文件的输入流
  6. private InputStream inputStream;
  7. //下载文件的类型
  8. private String contntType;
  9.  
  10. //创建InputStream输入流
  11. public InputStream getInputStream() throws FileNotFoundException {
  12. String path= ServletActionContext.getServletContext().getRealPath(inputPath);
  13. return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
  14. }
  15.  
  16. public void setInputStream(InputStream inputStream) {
  17. this.inputStream = inputStream;
  18. }
  19. public String getInputPath() {
  20. return inputPath;
  21. }
  22.  
  23. public void setInputPath(String inputPath) {
  24. this.inputPath = inputPath;
  25. }
  26.  
  27. public String getFileName() {
  28. return fileName;
  29. }
  30.  
  31. public void setFileName(String fileName) {
  32. this.fileName = fileName;
  33. }

struts.xml配置

  1. <action name="first" class="cn.happy.action.FileAction">
  2. <param name="inputPath">/upload</param>
  3. <result name="success" type="stream">
  4. <!-- 指定下载文件的内容类型,text/plain是默认类型 -->
  5. <param name="contntType">application/octet-stream</param>
  6. <!-- inputName默认值是inputStream,如果action中用于读取
  7. 下载文件内容的属性名是inputStream,那么可以省略这个参数 -->
  8. <param name="inputName">inputStream</param>
  9. <!--动态获取文件名,从Action中的取得filename-->
  10. <param name="contentDisposition">
  11. attachment;filename="${fileName}"
  12. </param>
  13. <param name="bufferSize"></param>
  14. </result>

struts.xml配置文件有几个地方我们要注意,首先是result的类型,以前我们定义一个action,result那里我们基本上都不写type属性,因为其默认是请求转发(dispatcher)的方式,除了这个属性一般还有redirect(重定向)等这些值,在这里因为我们用的是文件下载,所以type一定要定义成stream类型,告诉action这是文件下载的result,result元素里面一般还有param子元素,这个是用来设定文件下载时的参数,inputName这个属性就是得到action中的文件输入流,名字一定要和action中的输入流属性名字相同,然后就是contentDisposition属性,这个属性一般用来指定我们希望通过怎么样的方式来处理下载的文件,如果值是attachment,则会弹出一个下载框,让用户选择是否下载,如果不设定这个值,那么浏览器会首先查看自己能否打开下载的文件,如果能,就会直接打开所下载的文件,(这当然不是我们所需要的),另外一个值就是filename这个就是文件在下载时所提示的文件下载名字。

contentDispoistion参数由两部分组成,前面的部分表示处理文件的形式,如attachement表示在下载是弹出对话框,提示用户保存或者直接打开该文件;而后一部分表示下载文件的文件名称。两部分之间以;进行分隔。

在配置完这些信息后,我们就能过实现文件的下载功能了。

下载文件jsp页面

  1. <a href="first?fileName=ehcache.xml">点击此处下载文件</a>

struts2多文件上传:

其实多文件上传和单文件上传原理一样,单文件上传过去的是单一的File,多文件上传过去的就是一个List<File>集合或者是一个File[]数组,首先我们来看一下前端jsp部分的代码,

  1. <s:form action="more" enctype="multipart/form-data" method="POST">
  2. <s:file name="upload" label="选择文件"/><br>
  3. <s:file name="upload" label="选择文件"/><br>
  4. <s:submit name="submit" value="上传文件"/>
  5. </s:form>

file的名字必须都命名成upload才行,然后处理多文件上传的action代码如下:

  1. public class MoreUploadAction extends ActionSupport{
  2. //获取文件
  3. private List<File> upload;
  4. //获取文件的类型
  5. private List<String> uploadContentType;
  6. //上传文件的名称
  7. private List<String> uploadFileName;
  8. //获取文件上传的路径
  9. private String path;
  10.  
  11. @Override
  12. public String execute() throws Exception {
  13. byte[] bytes=new byte[];
  14. int index=;
  15. for (File item:upload) {
  16. FileInputStream inputStream=new FileInputStream(item);
  17. FileOutputStream output=new FileOutputStream(getPath()+"\\"+getUploadFileName().get(index++));
  18. int length=inputStream.read(bytes);
  19. while (length>){
  20. output.write(bytes,,length);
  21. length=inputStream.read(bytes);
  22. }
  23. output.flush();
  24. output.close();
  25. inputStream.close();
  26. }
  27. return SUCCESS;
  28. }
  29. //获取上传文件的保存路径
  30. //通过读取存放目录获得保存路径
  31. public String getPath() {
  32. return ServletActionContext.getServletContext().getRealPath(path);
  33. }
  34.  
  35. public void setPath(String path) {
  36. this.path = path;
  37. }
  38. public List<File> getUpload() {
  39. return upload;
  40. }
  41.  
  42. public void setUpload(List<File> upload) {
  43. this.upload = upload;
  44. }
  45.  
  46. public List<String> getUploadContentType() {
  47. return uploadContentType;
  48. }
  49.  
  50. public void setUploadContentType(List<String> uploadContentType) {
  51. this.uploadContentType = uploadContentType;
  52. }
  53.  
  54. public List<String> getUploadFileName() {
  55. return uploadFileName;
  56. }
  57.  
  58. public void setUploadFileName(List<String> uploadFileName) {
  59. this.uploadFileName = uploadFileName;
  60. }

struts.xml配置

  1. <!--多文件上传-->
  2. <action name="more" class="cn.happy.action.MoreUploadAction">
  3. <!--通过param参数设置保存目录的路径-->
  4. <param name="path">/upload</param>
  5. <result name="success">moreUpload/two.jsp</result>
  6. </action>

这样同样将其写到一个输出流里面,这样我们就可以在文件夹里看到上传的多个文件了

接下来的文件下载就和刚才的文件下载一模一样,struts.xml也是一样的,这里就不再重复了

总结:总的来说,struts2提供的文件上传下载机制简化了我们很多代码,我们可以在以后的项目中使用该机制,同样我们也可以使用FileUpload组件来进行文件的上传,这个都是因个人爱好决定!

struts2实现文件上传和下载的更多相关文章

  1. Struts2的文件上传与下载

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

  2. 【Struts2】文件上传与下载

    一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...

  3. (十五)struts2的文件上传和下载

    文件上传的原理 我们以前学习上传的时候知道需要将表单的enctype属性设置为multipart/form-data. 表单的enctype属性指定的是表单数据的编码方式,有三个值: -applica ...

  4. Struts2之文件上传与下载

    时间:2017-1-11 15:47 --使用commons-fileupload组件上传1.客户端    *   method="post"    *   <input t ...

  5. 11、Struts2 的文件上传和下载

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

  6. Struts2入门(七)——Struts2的文件上传和下载

    一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...

  7. Struts2 实现文件上传和下载

    实现上传功能 Struts2实际上是使用的commons fileupload 组件,所以记得导包哦. 1.首先你应该有一个上传页面 <!-- file的name属性与action中的File类 ...

  8. 7、Struts2实现文件上传和下载

    一.实现单个文件上传 1.创建如下web项目结构 2.在src下的com.action包下创建UploadAction.java package com.action; import java.io. ...

  9. struts2(六) 文件上传和下载

    前面对文件下载提过一点点,这里正好要讲文件上传,就放在一起在说一遍. --WH 一.单文件上传 在没学struts2之前,我们要写文件上传,非常麻烦,需要手动一步步去获取表单中的各种属性,然后在进行相 ...

随机推荐

  1. python yaml用法详解

    YAML是一种直观的能够被电脑识别的的数据序列化格式,容易被人类阅读,并且容易和脚本语言交互.YAML类似于XML,但是语法比XML简单得多,对于转化成数组或可以hash的数据时是很简单有效的. Py ...

  2. Socket编程 之使用fsockopen()函数

    fsockopen函数:初始化一个套接字连接到指定主机(hostname) get方式: client.php <?php //创建连接 $fp=fsockopen('localhost',80 ...

  3. js 空语句

    不写就行了 ){}

  4. vw、vh、vmin、vmax、em、rem的使用详解

    转载自:https://blog.csdn.net/ZNYSYS520/article/details/76053961 1,vw.vh.vmin.vmax 的含义 (1)vw.vh.vmin.vma ...

  5. python kline

    # -*- coding: utf-8 -*- # Qt相关和十字光标 from qtpy.QtGui import * from qtpy.QtCore import * from qtpy imp ...

  6. printf打印输出

    int PrintVal = 9;    /*按整型输出,默认右对齐*/    printf("%d\n",PrintVal);    /*按整型输出,补齐4位的宽度,补齐位为空格 ...

  7. MySQL的随机数函数rand()的使用技巧

    咱们学php的都知道,随机函数rand或mt_rand,可以传入一个参数,产生0到参数之间的随机整数,也可以传入两个参数,产生这两个参数之间的随机整数. 而在mysql里,随机数函数rand不能传参, ...

  8. 博客搬家一下到CSDN

    博客搬家一下到CSDN:http://blog.csdn.net/weixin_33409246

  9. 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/ ...

  10. Python的类的下划线命名的区别

    首先,单下划线开头,这个常被用于模块中,在一个模块中以单下划线开头的变量和函数被默认当做内部函数,如果使用from  module  import  *导入时,这部分变量和函数不会被导入.注意,如果使 ...