æç« åºå¤ï¼http://gznofeng.javaeye.com/blog/213851
Â
Java代ç
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;
import org.apache.struts2.ServletActionContext;
public class DownLoadAction extends BaseAction {
private String fileName;// åå§çéè¿paramæå®çæ件åå±æ§
private String inputPath;// æå®è¦è¢«ä¸è½½çæ件路å¾
public InputStream getInputStream() throws Exception {
System.out.println(getRequest().getParameter("fileName"));
// éè¿ ServletContextï¼ä¹å°±æ¯application æ¥è¯»åæ°æ®
System.out.println(fileName);
System.out.println(URLDecoder.decode(fileName));
return ServletActionContext.getServletContext().getResourceAsStream(inputPath+"//"+fileName);
}
public String execute() throws Exception {
return SUCCESS;
}
public void setInputPath(String value) {
inputPath = value;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
/** æä¾è½¬æ¢ç¼ç åçä¾ä¸è½½ç¨çæ件å */
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
}
XMLé ç½®ï¼
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="com.trendcom.uploadfile.action.DownLoadAction" namespace="/download" extends="struts-default">
<!-- æ件ä¸è½½ï¼æ¯æä¸æé件å -->
<action name="download"
class="com.trendcom.uploadfile.action.DownLoadAction">
<!-- åå§æ件åæ¾è·¯å¾ -->
<param name="inputPath">/WEB-INF/upload/</param>
<result name="success" type="stream">
<param name="contentType">
application/octet-stream;charset=ISO8859-1
</param>
<param name="inputName">inputStream</param>
<!-- 使ç¨ç»è¿è½¬ç çæ件åä½ä¸ºä¸è½½æ件åï¼downloadFileNameå±æ§
对åºactionç±»ä¸çæ¹æ³ getDownloadFileName() -->
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>
<!-- Add actions here -->
</package>
</struts>
Â