Action文件
public class DownloadAction extends ActionSupport {
private static final long serialVersionUID = 6329383258366253255L;
private String fileName;
public void setFileName() {
String fname = ServletActionContext.getRequest().getParameter("name");
try {
fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
this.fileName = fname;
System.out.println(fileName);
}
public String getFileName() throws UnsupportedEncodingException {
fileName = new String(fileName.getBytes(), "ISO-8859-1");
return fileName;
}
public InputStream getDownloadFile() {
this.setFileName();
return ServletActionContext.getServletContext().getResourceAsStream(
"/download/file/" + fileName);
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
Jsp文件
<body>
<%
String ipath = request.getRealPath("/Files");
File file = new File(ipath);
File[] files = file.listFiles();
for(int i=0;i<files.length;i++){
String fname = files[i].getName();
fname = java.net.URLEncoder.encode(fname,"utf-8");
out.println(files[i].getName());
out.println("<a href='Down.action?name="+fname+"'>"+files[i].getName()+"</a><br />");
}
%>
</body>
Struts.xml文件
<action name="Down" class="action.DownAction">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
在下載時一直是Down.txt IE是down.action
看看你的struts.xml裡面,在result結果集裡面的,名字為contentdisposition參數標簽裡面的文件名是否用了ognl表達式來動態獲取文件名