首页>代码>Struts2实现文件上传下载>/FileUpAction/src/Demo/downFile.java
package Demo;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class downFile extends ActionSupport{
	
	public String list() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		File file = new File(path);
		String[] fileNames = file.list();
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> request = (Map<String, Object>) ac.get("request");
		request.put("fileNames", fileNames);
		return "list";
	}
	private String fileName;
	
	public void setFileName(String fileName) {
		try {
			fileName = new String(fileName.getBytes("ISO8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.fileName = fileName;
	}
	
	public InputStream getAttrInputStream(){
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileName);
	}
	
	public String getDownFileName(){
		try {
			fileName = URLEncoder.encode(fileName,"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fileName;
	}
	
	public String down(){
		return "down";
	}
}
最近下载更多
xmx123  LV5 2023年9月14日
思_无邪  LV6 2022年11月24日
Jjjj1111  LV5 2021年12月30日
冯少伟  LV6 2020年12月8日
wydpopwyd  LV11 2020年11月24日
最代码官方  LV167 2020年11月22日
最近浏览更多
LARY  LV1 1月5日
WBelong  LV7 2023年12月19日
uni-code_0123  LV1 2023年12月7日
abcd20231129 2023年11月27日
暂无贡献等级
xmx123  LV5 2023年9月14日
小屁孩  LV7 2023年6月2日
Super强  LV13 2023年6月2日
暂无贡献等级
sweetlove  LV17 2022年11月28日
思_无邪  LV6 2022年11月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友