<action name="download" method="fileLoad" class="com.gnet.todometters.fileDownloadAction">
<result name="success" type="stream">
                <param name="contentType">application/octet-stream;charset=utf-8</param>
                <param name="contentDisposition">attachment;filename="${nfileName}"</param>
                <param name="inputName">downloadFile</param>
            </result>
</action>
action:
public class FileDownloadAction extends BaseAction{

private String nfileName;

public InputStream getDownloadFile() throws Exception {
HttpServletRequest request=ServletActionContext.getRequest();
String folder = ServletActionContext.getServletContext().getRealPath("/upFile");
String realPath=folder+"\\"+nfileName;
return new FileInputStream(new File(realPath));
// return ServletActionContext.getServletContext().getResourceAsStream(realPath);
} // 如果下载文件名为中文,进行字符编码转换
public String getDownloadChineseFileName() throws UnsupportedEncodingException {
String downloadChineseFileName = nfileName; try {
downloadChineseFileName = new String(downloadChineseFileName
.getBytes(), "gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} return downloadChineseFileName;
} public String fileLoad(){
return SUCCESS;
} public String getNfileName() {
String downloadChineseFileName = nfileName; try {
downloadChineseFileName = new String(downloadChineseFileName
.getBytes(), "iso8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} return downloadChineseFileName;
} public void setNfileName(String nfileName) {
this.nfileName = nfileName;
}
错误我看过了,值没有从配置文件传到action中,不知道为什么,急求,谢谢....

解决方案 »

  1.   

    <param name="inputName">downloadFile</param>downloadFile没赋值。public InputStream getDownloadFile() throws Exception {
    HttpServletRequest request=ServletActionContext.getRequest();
    String folder = ServletActionContext.getServletContext().getRealPath("/upFile");
    String realPath=folder+"\\"+nfileName;
    downloadFile = new FileInputStream(new File(realPath));
    return downloadFile
    // return ServletActionContext.getServletContext().getResourceAsStream(realPath);
    }
      

  2.   


       public String download() throws Exception {
          try {
             String filePath = servletRequest.getParameter("filePath");
             File file = new File(filePath);
             String fileName = file.getName().replaceAll("\\[.+\\]", "");
             fileName = URLEncoder.encode(fileName, "utf-8");
             fileName = fileName.replace('+', ' ');
             servletResponse.setContentType("application/x-download");
             servletResponse.addHeader("Content-Disposition",
                   "attachment;filename=" + fileName);         excelStream = new FileInputStream(file);
          }
          catch(Exception e) {
             write(Tool.getErrorMsg(e.getMessage()));         return ERROR;
          }      return SUCCESS;
       }
      

  3.   


    <action name="search_export"
    class="com.xxx.hdm.servlet.Search" method="export">
    <result name="success" type="stream">
    <param name="inputName">excelStream</param>
    </result>
    <result name="error">search_detail.jsp</result>
    </action>