struts2 文件上传下载注解示例

上传下载网上有很多,在这里就不一一介绍了,写这个博客的目的是因为写项目的时候一时间找不到适用的注解形式的下载(操作本地文件的显示)

 

 

代码如下:

package com.carrentmanager.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


@Controller
@Scope("prototype")
@Namespace("/download")
/***params键值对形式***/
@Result(name = "download",type = "stream", params={"contentType","images/jpg","inputName","targetFile","contentDisposition","fileName=%{fileName}","bufferSize","4096"})
public class DownloadAction extends BaseAction{
/**
*
*/
  private static final long serialVersionUID = 1L;
  private String fileName;
  private String filePath;
  private String savePath = "D:/images_day_57/";
  @SuppressWarnings("unused")
  private InputStream targetFile;

  @Action(value="download")
  public String execute(){
    return "download";
  }

  public InputStream getTargetFile() throws FileNotFoundException {
    return new FileInputStream(savePath + filePath);
  }

  public String getFilePath() {
    return filePath;
  }
  public void setFilePath(String filePath) {
    this.filePath = filePath;
  }
  public String getFileName() {
    return fileName;
  }
  public void setFileName(String fileName) {
    this.fileName = fileName;
  }
  public String getSavePath() {
    return savePath;
  }
  public void setSavePath(String savePath) {
    this.savePath = savePath;
  }
  public void setTargetFile(InputStream targetFile) {
    this.targetFile = targetFile;
  }
}

转载于:https://www.cnblogs.com/bigot-Lee/p/6807467.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值