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;
  }
}

posted @ 2017-05-04 15:39  bigot_Lee  阅读(623)  评论(0)    收藏  举报