<a href="javascript:location.href='downFile?aname='+encodeURIComponent(' ${c.filName}');">${c.filName}</a>
JSP页面利用encodeURIComponent进行编码。传到action之后。在对其进行解码。代码如下:
String filenames = URLDecoder.decode(URLDecoder.decode(ServletActionContext.getRequest().getParameter("aname"),"UTF-8")).trim();(SSH框架)
后台代码(SSH框架注释):@Action(value = "/downFile", results = {
@Result(name = SUCCESS , type="stream",location = "manageFile",params={"contentType","application/octet-stream",
"inputName","inputStream",
"contentDisposition","attachment;filenames=${filenames}",
"bufferSize","4096"}),
@Result(name = ERROR, location = "/no.jsp") })
public String downFile() throws UnsupportedEncodingException{
System.out.println("测试进入下载action");
String filenames = URLDecoder.decode(URLDecoder.decode(ServletActionContext.getRequest().getParameter("aname"),"UTF-8")).trim();
System.out.println("ceshi1"+filenames+"测试");
File dir=new File(ServletActionContext.getServletContext().getRealPath("files"));
//判断文件是否上传,如果上传的话将会创建该目录
if(!dir.exists()){
dir.mkdir(); //创建该目录
}
inputStream=ServletActionContext.getServletContext().getResourceAsStream("/files/"+filenames);
if(inputStream!=null){
System.out.println(inputStream);
return SUCCESS;}
else{
return ERROR;
}
}