Struts2下载与上传

这篇博客同时也在我的个人网站上有,由于想让更多人看到,所以也在这里发一下

我的个人网站:www.wenjingyi.top

下载

直接上步骤
  1. 在Action类中准备文件名
  2. 在Action类中准备文件流
  3. 配置struts.xml文件
文件名
最好在Action类中给出,再让struts.xml文件得到这个文件名。
文件流
既然是下载,那么文件流就必不可少。

将Action写成以下模样
package me.paul.strutsLearning.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author paul
 * @since 2016-11-08
 * Example Download Action
 * */
public class DownloadAction extends ActionSupport{	
  private static final long serialVersionUID = 1L;	

  //文件名,用于在用户下载时显示并作为下载文件的名字
  private String fileName;	
  //比不可少的get,struts.xml中要用到
  public String getFileName(){
    return fileName;
  }	
  //在用户的请求中得到fileName
  public void setFileName(String fileName) {
    this.fileName = fileName;
  }
  //文件流
  public InputStream getDownloadStream() throws FileNotFoundException{
    //获得/WEB-INF/file/的绝对路径
    String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/file/"); 
    //得到对应的文件
    File file = new File(path,fileName);
    //创建流
    return new FileInputStream(file);
  }
  
  //简单地返回结果
  public String download(){
    return "download";
  }

}
配置struts.xml
添加以下Action
<action name="download" class="me.paul.strutsLearning.action.DownloadAction" method="download">
  <result name="download" type="stream">
    <!--得到文件流,要求action类中有getDownloadStream方法-->
    <param name="inputName">downloadStream</param>
    <!--指定contentType , octet-stream是二进制数据-->
    <param name="contentType">octet-stream</param>
    <!--指定下载的文件名,要求action类中有getFileName方法-->
    <param name="contentDisposition">attachment;fileName=${fileName}</param>
    <!--缓存区大小-->
    <param name="bufferSize">1024</param>
  </result>
</action>

配置完,启动tomcat,在浏览器中输入,http://localhost:8080/xxx/download?fileName=hosts,访问则会提示下载。这个下载要求服务器中xxx项目中存在/WEB-INF/file/hosts文件

往result稍稍深入了解。下载的处理result是stream,在 struts-default.xml配置文件中可以找到对应的类为 :org.apache.struts2.result.StreamResult
打开对应源码,会看见这些属性。
protected String contentType = "text/plain";
protected String contentLength;
protected String contentDisposition = "inline";
protected String contentCharSet ;
protected String inputName = "inputStream";
protected InputStream inputStream;
protected int bufferSize = 1024;
protected boolean allowCaching = true;
这些就是在配置struts.xml的时候可以覆盖的属性。当然并不是所有属性都需要覆盖。在这个类的类文档中给出了一个例子。
 * <result name="success" type="stream">
 *   <param name="contentType">image/jpeg</param>
 *   <param name="inputName">imageStream</param>
 *   <param name="contentDisposition">attachment;filename="document.pdf"</param>
 *   <param name="bufferSize">1024</param>
 * </result>

上传

  1. Action类中要有File类型的属性,及其setter
  2. jsp页面表单
  3. 可选地,在struts.xml中配置constant,以改变上传的限制大小
Action类
package me.paul.strutsLearning.action;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author paul
 * @since 2016-11-08
 * 
 *        Upload Example
 * 
 * */
public class UploadAction extends ActionSupport {
  private static final long serialVersionUID = 1L;

  //保存的路径
  private String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/file/");
  
  //struts的拦截器会把request中的文件保存到这个文件,这个文件的名字要与上传表单中的name属性一致
  private File file1;

  //得到文件的名字
  private String file1FileName;
  //得到文件的类型
  private String file1ContentType;

  //set and get ers
  public File getFile1() {
    return file1;
  }
  public void setFile1(File file1) {
    this.file1 = file1;
  }
  public String getFile1FileName() {
    return file1FileName;
  }
  public void setFile1FileName(String file1FileName) {
    this.file1FileName = file1FileName;
  }
  public String getFile1ContentType() {
    return file1ContentType;
  }
  public void setFile1ContentType(String file1ContentType) {
    this.file1ContentType = file1ContentType;
  }

  //简单地把上传得到的文件复制到对应的目录下,借助了commons-io的FileUtils
  public String upload() throws IOException {
    File destFile = new File(path,file1FileName);
    FileUtils.copyFile(file1, destFile);
    return "index";
  }

}
表单
表单中有几点需要注意
  1. method需要为POST
  2. enctype需要为multipart/form-data
  3. 对应文件input的name属性要和Action类中的File对象名对应
<form action="${pageContext.request.contextPath }/upload" method="POST" enctype="multipart/form-data">
  选择文件 <input type="file" name="file1">
  <input type="submit" value="上传">
</form>
最后一点,struts2上传默认的限制大小为2M,如果需要改变这个大小可以在struts.xml文件中配置
<struts>
  <constant name="struts.multipart.maxSize" value="10485760"></constant>

  <package name="strutsLearning" extends="struts-default">
位置在struts节点下。constant常量名可以在 struts.properties配置文件中找到。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值