package org.great.util.file.filedownload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.great.util.file.common.Config;
import org.great.util.file.common.Util;
import com.opensymphony.xwork2.Action;
public class FileDownloadAction implements Action {
private Log log = LogFactory.getLog(this.getClass());
private List<String> fnameList;
private String keyFileName;
private String inputPath;
public String getInputPath() {
return inputPath;
}
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public void setInputPath(String value) {
inputPath = value;
}
public String getKeyFileName() {
return keyFileName;
}
public void setKeyFileName(String keyFileName) {
this.keyFileName = keyFileName;
}
public List<String> getFnameList() {
return fnameList;
}
public void setFnameList(List<String> fnameList) {
this.fnameList = fnameList;
}
/**
* 下载文件
*/
public String execute() throws Exception {
log.info("下载文件:"+keyFileName);
return SUCCESS;
}
/**
* 列出可下载文件列表
* @return
* @throws Exception
*/
public String listDownFile() throws Exception {
ServletContext context = ServletActionContext.getServletContext();
Util util = new Util();
String filepath = util.convertPath(context);
File dir = new File(filepath);
String[] fnames = null;
if (key != null) {
fnames = dir.list(new MyFileFilter(key));
} else {
fnames = dir.list();
}
if (fnames != null && fnames.length > 0) {
fnameList = new ArrayList<String>();
for (String f : fnames) {
fnameList.add(f);
}
}
return SUCCESS;
}
public InputStream getInputStream() throws Exception {
InputStream is = null;
Util util = new Util();
String inputPath = util.convertPath(ServletActionContext.getServletContext());
is = new FileInputStream(inputPath + "/" + keyFileName);
return is;
}
// 如果下载文件名为中文,进行字符编码转换
public String getKeyFile() {
String downloadChineseFileName = keyFileName;
try {
downloadChineseFileName = new String(
downloadChineseFileName.getBytes("gbk"), "iso-8859-1");
} catch (Exception e) {
log.info("return getKeyFile.", e);
}
return downloadChineseFileName;
}
}
评论3
最新资源