struts2实现文件上传下载

1、首先导入commons-io-1.3.2.jar还有commons-fileupload-1.2.1.jar两个jar包,然后在web.xml文件中加入如下代码:

<filter>

       <filter-name>struts</filter-name>

    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

    </filter>

       <filter-mapping>

       <filter-name>struts</filter-name>

       <url-pattern>/*</url-pattern>  

    </filter-mapping>

<filter>

       <filter-name>struts-cleanup</filter-name>    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

    </filter>

    <filter-mapping>

       <filter-name>struts-cleanup</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

2、

写上传的index.jsp文件:

 <form action="upload.action"method="post"

  enctype="multipart/form-data">

   <input type="text"name="title"/>

   <br>

   <input type="file"name="upload"/>

   <br>

   <input value="upload"type="submit"/>

  </form>

3、

Action代码:

publicclass UploadAction extends ActionSupport {

 private String title;//文件标题,对应form表单的title

 private File upload;//文件内容,对应form表单的upload

 private String uploadContentType;//文件类型

 private String uploadFileName;//文件名

 private String allowTypes;//允许的类型

 private String savePath;//保存路径

 // 接受依赖注入的方法

 publicvoid setSavePath(String value) {

  this.savePath = value;

 }

 private String getSavePath() throws Exception {

  return ServletActionContext.getServletContext().getRealPath(savePath);

 }

 publicvoid setTitle(String title) {

  this.title = title;

 }

 publicvoid setUpload(File upload) {

  this.upload = upload;

 }

 publicvoid setUploadContentType(String uploadContentType) {

  this.uploadContentType = uploadContentType;

 }

 publicvoid setUploadFileName(String uploadFileName) {

  this.uploadFileName = uploadFileName;

 }

 public String getTitle() {

  return (this.title);

 }

 public File getUpload() {

  return (this.upload);

 }

 public StringgetUploadContentType() {

  return (this.uploadContentType);

 }

 public String getUploadFileName(){

  return (this.uploadFileName);

 }

 @Override

 public String execute() throws Exception {

  System.out.println("开始上传单个文件---");

  System.out.println(getSavePath());

  System.out.println("==========" + getUploadFileName());

  System.out.println("==========" + getUploadContentType());

  System.out.println("==========" + getUpload());

 

 

  // 以服务器的文件保存地址和原文件名建立上传文件输出流

  FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"

    + getUploadFileName());

  FileInputStream fis = new FileInputStream(getUpload());

  byte[] buffer = newbyte[1024];

  int len = 0;

  while ((len = fis.read(buffer))> 0) {

   fos.write(buffer, 0, len);

  }

  returnSUCCESS;

 }

 public String filterType(String[]types) {

  String fileType = this.getUploadContentType();

  for (String type : types) {

   if (type.equals(fileType)) {

    returnnull;

   }

  }

  returnINPUT;

 }

 public String getAllowTypes() {

  returnallowTypes;

 }

 publicvoid setAllowTypes(String allowTypes) {

  this.allowTypes =allowTypes;

 }

}

4、

Struts.xml代码:

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

 <constant name="struts.custom.i18n.resources"

 value="globalMessages"/>

 <constant name="struts.i18n.encoding"value="GBK"/>

 <package name="lee"extends="struts-default">

  <action name="upload"class="UploadAction">

   <param name="savePath">/upload</param>

   <result>/success.jsp</result>

   <result name="input">/index.jsp</result>

  </action>

 </package>

5、

返回结果的success.jsp:

 <body>

  上传成功!

  <br>

  文件标题:

  <s:property value=" +title" />

  <br>

  文件为:

 <s:property value="'upload/'+ uploadFileName"/>

 

  <br>

 </body>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值