📄 filedownaction.java
字号:
package com.action;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.model.TFile;
import com.opensymphony.xwork2.ActionSupport;
import com.service.FileService;
public class FileDownAction extends ActionSupport {
/**
* 下载ACTION
*/
private static final long serialVersionUID = 1L;
private int id;
protected FileService fileService;
private String fileName;
private String inputPath;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setFileService(FileService fileService) {
this.fileService = fileService;
}
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
/**
* 得到请求下载的文件名
* 把随机产生的文件名赋给fileName
* */
public void setFileName(){
TFile tfile=fileService.findFileById(id);
this.fileName = tfile.getFileGname();
}
/**
* @getFileName
* 此方法对应的是struts.xml文件中的:
* <param name="contentDisposition">attachment;filename="${fileName}"</param>
* 这个属性设置的是下载工具下载文件时显示的文件名,
* 要想正确的显示中文文件名,我们需要对fileName再次编码
* 否则中文名文件将出现乱码,或无法下载的情况
* */
public String getFileName() throws UnsupportedEncodingException {
TFile tfile=fileService.findFileById(id);
fileName=new String(tfile.getFileName().getBytes("GBK"),"ISO8859-1");
System.out.println(fileName);
return fileName;
}
/**
* @getDownloadFile
* 此方法对应的是struts.xml文件中的:
* <param name="inputName">downloadFile</param>
* 返回下载文件的流,可以参看struts2的源码
* */
public InputStream getinputStream(){
this.setFileName();
return ServletActionContext.getServletContext().getResourceAsStream(inputPath+fileName);
}
public String execute(){
return SUCCESS;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -