1、運用struts的下載類DownloadAction
2、直接流
1、運用struts的下載類DownloadAction
參考:
http://blog.csdn.net/chenyanbo/archive/2011/01/20/6154260.aspx
- package cn.finefuture.common.faxserver.struts;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DownloadAction;
- import cn.finefuture.common.faxserver.service.FaxServerService;
- import cn.finefuture.common.faxserver.service.impl.FaxServerServiceImp;
- public class FaxDownAction extends DownloadAction {
- private FaxServerService faxServerService = new FaxServerServiceImp();
- public void setFaxServerService(FaxServerService faxServerService) {
- this .faxServerService = faxServerService;
- }
- @Override
- protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- // 傳真編號
- String faxId = request.getParameter( "faxId" );
- InputStream inStream = null ;
- if (faxId != null && !(faxId.equals( "" ))) {
- //獲取InputStream
- inStream = this .faxServerService.getFaxInAtt(faxId);
- }
- final InputStream in = inStream;
- final String contentType = "application/file" ;
- // 建
- // 無法在保存文件對話框中顯示正確的文件名
- response.setHeader( "content-disposition" , "attachment; filename="
- + faxId);
- return new DownloadAction.StreamInfo() //使用隱式的方法實現了StreamInfo接口
- {
- public String getContentType() {
- return contentType;
- }
- public InputStream getInputStream() throws IOException {
- return in;
- }
- };
- }
- // 如果Struts動作不加file請求參數,則通過execute方法將指定目錄中文件列表輸出到客戶端
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- // 當file參數存在時,則調用DownloadAction中的execute方法
- // 實際上,在DownloadAction類中的execute方法調用了getStreamInfo方法
- // 這條語句就相當於調用了getStreamInfo方法
- return super .execute(mapping, form, request, response);
- }
- }
2、第二種自己寫輸出流
- /**
- * 下載附件
- *
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward doDownLoadAttachment(ActionMapping mapping,
- ActionForm form, HttpServletRequest request,
- HttpServletResponse response) {
- // 傳真編號
- String faxId = request.getParameter( "faxId" );
- InputStream inStream = null ;
- if (faxId != null && !(faxId.equals( "" ))) {
- inStream = this .faxServerService.getFaxInAtt(faxId);
- }
- // 流不等於null
- if (inStream != null ) {
- try {
- // 設置輸出的格式
- response.reset();
- response.setContentType( "bin" );
- // 給中文字符轉碼
- faxId = new String(faxId.getBytes( "GBK" ), "iso8859-1" );
- // 設置文件名
- response.setHeader( "Content-Disposition" ,
- "attachment;filename=" + faxId);
- // 循環取出流中的數據
- byte [] b = new byte [ 1024 ];
- int len;
- while ((len = inStream.read(b)) > 0 ) {
- response.getOutputStream().write(b, 0 , len);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- inStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return null ;
- }
參考:
http://blog.csdn.net/chenyanbo/archive/2011/01/20/6154260.aspx