而是必須實做 org.apache.struts.actions.DownloadAction 並實現 getStreamInfo() method
如果你想要自行設定緩衝大小的話 你也可以覆寫 getBufferSize() method
而 Strust 預設的緩衝大小為 4096
接著來介紹兩種不同的做法來實現 Strust 的檔案下載 同樣都是 implements DownloadAction
1. FileStreamInfo
主要是針對主機上的檔案
2. ResourceStreamInfo
主要是針對你的 Web AP 的路徑下的檔案
以下個別來看看他的範例
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
// 此範例展示下載一個本地端的 PDF 檔
public class FileDownloadAction extends DownloadAction{
protected StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String contentType = "application/pdf";
File file = new File("c:\\class/pdf");
return new FileStreamInfo(contentType, file); //FileStreamInfo
}
}
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
// 此範例展示下載一個web ap 的 PDF 檔
public class ExampleResourceDownload extends DownloadAction {
protected StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String contentType = "application/pdf";
String path = "/file/pdf/class.pdf";
ServletContext application = servlet.getServletContext();
return new ResourceStreamInfo(contentType, application, path); //ResourceStreamInfo
}
}
到這邊看到 Struts 在整理下載檔案這部分 其實很簡單
你不需要理會太多的表頭設定 你只需要告訴 Struts 要下載的檔案的 MIME Type 即可
沒有留言:
張貼留言