2013-07-04 51 views
2

目前我想下载的文件名称为“downloadStatistic.html”。我想把它改成“无论我想要什么”。顺便说一下,它是一个* .csv文件。如何更改struts中的下载文件名称

这里是我的下载操作的代码:

@Scope("prototype") 
@Component("/downloadStatistic") 
public class StatisticDownloadAction extends DownloadAction { 

@Override 
protected StreamInfo getStreamInfo(ActionMapping actionMapping, 
            ActionForm actionForm, 
            HttpServletRequest httpServletRequest, 
            HttpServletResponse httpServletResponse) throws Exception { 

    String filepath = httpServletRequest.getParameter("filepath"); 
    String contentType = "text/comma-separated-values"; 

    File file = new File(filepath); 

    return new FileStreamInfo(contentType, file); 
    } 
} 

这就是我的支柱XML:

<action 
      path="/shipmentStatistic" 
      name="shipmentStatisticForm" 
      scope="request" 
      validate="false" 
      type="de.logentis.struts.DelegatingActionProxy" 
      input="/jsp/statistics/ShipmentStatistic.jsp"> 
     <forward name="back" path="/menu.html" redirect="true"/> 
     <forward name="download" path="/downloadStatistic.html?filepath={0}" redirect="true"/> 
    </action> 

    <action 
      path="/downloadStatistic" 
      type="de.logentis.struts.DelegatingActionProxy"> 
     <forward name="back" path="/shipmentStatistic.html" redirect="true"/> 
    </action> 

我在哪里可以定义下载文件的文件名?

回答

1

或许你尝试以下方法:

public class DownloadFileAction extends Action{ 

@Override 
public ActionForward execute(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) 
throws Exception { 

    String filepath = httpServletRequest.getParameter("filepath"); 
    response.setContentType("application/octet-stream"); 
    response.setHeader("Content-Disposition","attachment;filename=statistic.csv"); 

    try 
    { 
    FileInputStream in = 
     new FileInputStream(new File(filepath)); 
    ServletOutputStream out = response.getOutputStream(); 
    byte[] outputByte = new byte[4096]; 
    while(in.read(outputByte, 0, 4096) != -1){ 
    out.write(outputByte, 0, 4096); 
    } 
    in.close(); 
    out.flush(); 
    out.close(); 

}catch(Exception e){ 
    e.printStackTrace(); 
} 

return null; 
} 
}  

或者:

@Override 
public ActionForward execute(ActionMapping mapping, ActionForm form, 
         HttpServletRequest request, HttpServletResponse response) 
    throws Exception { 

String filepath = request.getParameter("filepath"); 
File statisticFile = new File(filepath); 

byte[] ourArray = new byte[Integer.parseInt(statisticFile.length() + "")]; 

response.setContentType("application/octet-stream"); 
response.setHeader("Content-Disposition", "attachment;filename=" + statisticFile.getName()); 

FileInputStream in = new FileInputStream(statisticFile); 
ServletOutputStream out = response.getOutputStream(); 

try { 
    int c; 
    while ((c = in.read()) != -1) { 
     out.write(c); 
     out.flush(); 
    } 
} finally { 
    if (in != null) { 
     in.close(); 
    } 
    if (out != null) { 
     out.close(); 
    } 
} 
return null; 
} 
+0

工程...谢谢 –

+0

我们随时欢迎您:] –