<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0, comments - 77, trackbacks - 0, articles - 356
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    StrutsFileDownload-----DownloadAction

    Posted on 2007-01-03 14:05 semovy 閱讀(1481) 評論(3)  編輯  收藏 所屬分類: struts

    StrutsFileDownload

    A new DownloadAction was added in Struts 1.2.6 (see the [WWW] JavaDoc). This page is to show how to use it and has the following structure:

    1. Implementing a DownloadAction
      1. Implement the getStreamInfo() Method
      2. Implement the getBufferSize() Method
    2. Examples
      1. FileStreamInfo Example
      2. ResourceStreamInfo Example
      3. Byte Array Example
    3. Using the DownloadAction in your Web Pages
    4. Content Disposition
      1. Setting the Content Disposition
      2. Content Disposition Values

    (!) FrankZ: Here is the sample webapp... downloadapp.zip Please note that this is my first ever attempt at editing a Wiki entry, so if I screwed anything up, I apologize in advance! Lastly, I skimmed through the content of this entry and didn't see anything blatantly wrong. I will try and proof it more thoroughly as time allows, but for now, in addition to the sample app, it should get anyone going in the right direction.

    <!> niallp: Since I haven't actually used this class myself, I'm hoping that Martin, Frank or anyone else involved in the discussion/creation of this would take a look here to see if its OK.

    (!) Details from this thread: [WWW] Mail Archive - Note that the link to the sample app at omnytex.com referenced in this thread is no longer valid. The sample app is attached to this Wiki page only, so download it from here downloadapp.zip

    Implementing a DownloadAction

    You need to extend org.apache.struts.actions.DownloadAction and implement the getStreamInfo() method. Optionally you can also override the getBufferSize() method if you require a different buffer size from the default.

    Implement the getStreamInfo() Method

    The getStreamInfo() method returns a StreamInfo object - which is an inner interface of the DownloadAction. The DownloadAction provides two concrete implementations (static inner classes) of the StreamInfo interface:

    • FileStreamInfo - Simplifies downloading of a file from disk - need to pass a java.io.File object to the constructor along with the content type.

    • ResourceStreamInfo - simplifies downloading of a web application resource - need to pass the ServletContext, path and content type to its constructor.

    In the examples below, I have also provided a Byte array implementation of the StreamInfo interface.

    Implement the getBufferSize() Method

    The DownloadAction, by default, returns a buffer size of 4096. Optionally, this may be overriden to customize the size of the buffer used to transfer the file.

    Examples

    Below are three examples:

    • using a File

    • using a web application resource

    • using a byte array.

    FileStreamInfo Example

    Example of using the DownloadAction with a file. This example picks up the file name from the parameter attribute in the strust-config.xml action mapping.

    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;
    
    public class ExampleFileDownload extends DownloadAction{
    
        protected StreamInfo getStreamInfo(ActionMapping mapping, 
                                           ActionForm form,
                                           HttpServletRequest request, 
                                           HttpServletResponse response)
                throws Exception {
            
            // Download a "pdf" file - gets the file name from the
            // Action Mapping's parameter
            String contentType = "application/pdf";
            File file          = new File(mapping.getParameter());
    
            return new FileStreamInfo(contentType, file);
            
        }
    
    }
    
    

    ResourceStreamInfo Example

    Example of using the DownloadAction with a web application resource. This example picks up the web application resource path from the parameter attribute in the strust-config.xml action mapping.

    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;
    
    public class ExampleResourceDownload extends DownloadAction {
    
        protected StreamInfo getStreamInfo(ActionMapping mapping, 
                                           ActionForm form,
                                           HttpServletRequest request, 
                                           HttpServletResponse response)
                throws Exception {
            
            // Download a "jpeg" file - gets the file name from the
            // Action Mapping's parameter
            String contentType         = "image/jpeg";
            String path                = mapping.getParameter();
            ServletContext application = servlet.getServletContext();
            
            
            return new ResourceStreamInfo(contentType, application, path);
            
        }
    
    }
    
    
    

    Byte Array Example

    Example of using the DownloadAction with a Byte Array.

    This example creates a ByteArrayStreamInfo inner class which implements the StreamInfo interface.

    (niallp: IMO we should include this ByteArrayStreamInfo in the implementation)

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ByteArrayInputStream;
    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;
    
    public class ExampleByteArrayDownload extends DownloadAction {
    
        protected StreamInfo getStreamInfo(ActionMapping mapping, 
                                           ActionForm form,
                                           HttpServletRequest request, 
                                           HttpServletResponse response)
                throws Exception {
            
            // Download a "pdf" file
            String contentType = "application/pdf";
            byte[] myPdfBytes  = null;              // Get the bytes from somewhere
    
            return new ByteArrayStreamInfo(contentType, myPdfBytes);
            
        }
    
        protected class ByteArrayStreamInfo implements StreamInfo {
            
            protected String contentType;
            protected byte[] bytes;
            
            public ByteArrayStreamInfo(String contentType, byte[] bytes) {
                this.contentType = contentType;
                this.bytes = bytes;
            }
            
            public String getContentType() {
                return contentType;
            }
    
            public InputStream getInputStream() throws IOException {
                return new ByteArrayInputStream(bytes);
            }
        }
    }
    
    

    Using the DownloadAction in your Web Pages

    The last bit of the puzzle is how do I use this action?

    You need to do two things:

    • As with any Struts action, you need to configure it in the struts-config.xml

    • Use it on your web page like any other link to a file

    So for example you might have something like the following in your struts-config.xml:

        <action path="/downloadMyPdfFile" type="myPackage.ExampleFileDownload" parameter="/foo/bar.pdf">
        <action path="/downloadMyImage"   type="myPackage.ExampleResourceDownload" parameter="/images/myImage.jpeg">
    
    

    The on your jsp page, you might use these in the following way:

        <html:img action="downloadMyImage" alt="My Image" height="400" width="400"/>
    
        <html:link action="downloadMyPdfFile">Click Here to See the PDF</html:link>
    
    

    Note you may need to set the nocache value to false in the controller section of your struts config file. Nocache set to true prevented me from being able to use Internet Explorer to download the file succesfully; Firefox and Safari worked fine.

        <controller contentType="text/html;charset=UTF-8" locale="true" nocache="false" />
    

    Content Disposition

    Setting the Content Disposition

    DownloadAction doesn't cater for setting the content dispositon header. The easiest way is set it in the getStreamInfo() method, for example...

    public class ExampleFileDownload extends DownloadAction{
    
    
        protected StreamInfo getStreamInfo(ActionMapping mapping, 
                                           ActionForm form,
                                           HttpServletRequest request, 
                                           HttpServletResponse response)
                throws Exception {
    
            // File Name
            String fileName = mapping.getParameter();
    
            // Set the content disposition
            response.setHeader("Content-disposition", 
                               "attachment; filename=" + fileName);
            
            // Download a "pdf" file - gets the file name from the
            // Action Mapping's parameter
            String contentType = "application/pdf";
            File file          = new File(fileName);
    
            return new FileStreamInfo(contentType, file);
            
        }
    }
    
    

    Probably would want to play with the file name, to remove any path info first.

    Content Disposition Values

    You can set the content disposition to either download the file or open up in the browser:

    • To open up in the browser: "inline; filename=myFile.pdf"

    • To download: "attachment; filename=myFile.pdf"

    Displaying images I always set the content disposition to the "inline" option.

    NOTE: I'm not expert at this, just got it working by trial and error. I had problems and I'm seem to remember I had to play with setting the response headers to get it to work properly. There may also be browser issues - my app only has to work with IE. Anyone who knows more about this, feel free to amend this page.

    FIX for IE: Content-Disposition Attachment Header Does Not Save File [WWW] link

    last edited 2006-01-25 18:40:18 by WendySmoak


    評論

    # re: StrutsFileDownload-----DownloadAction  回復  更多評論   

    2009-04-16 13:50 by famingyuan
    sorry,I think the third method is not very good!

    the process is: File ---FileInputStream---ByteArrayOutputStream---

    ByteArray---BufferedArrayInputStream at last the file return as a

    InputStream ,So why not at first make file to InputStream to return

    # re: StrutsFileDownload-----DownloadAction  回復  更多評論   

    2009-04-16 13:51 by famingyuan
    but I still thank you very much!

    you let me know much! thank you very very very much!

    # re: StrutsFileDownload-----DownloadAction  回復  更多評論   

    2009-04-16 13:54 by famingyuan
    I will offen come to see you homepage!

    I will always expect you bring us more hard-won and import information about java and java web !

    thanks again!
    主站蜘蛛池模板: 一个人免费视频观看在线www| 国产高清视频在线免费观看| 久久久精品国产亚洲成人满18免费网站| 国产成人免费全部网站| 亚洲AV无码国产精品色| 黄在线观看www免费看| 亚洲最大黄色网站| 一本岛高清v不卡免费一三区| 拔擦拔擦8x华人免费久久| 亚洲日产乱码一二三区别| 成年女人午夜毛片免费看| 亚洲人成77777在线播放网站不卡| 亚洲免费视频一区二区三区| 亚洲综合图色40p| 国产免费无码一区二区| 亚洲精品乱码久久久久久下载| 亚洲成a人无码亚洲成www牛牛| 三上悠亚电影全集免费| 久久夜色精品国产亚洲AV动态图| 亚洲精品乱码久久久久蜜桃 | 亚洲色在线无码国产精品不卡| 国产精品无码免费专区午夜| 久久久久亚洲AV无码专区网站| 亚洲老熟女五十路老熟女bbw| 精品国产一区二区三区免费| 精品亚洲国产成AV人片传媒| 永久免费毛片在线播放| 久久水蜜桃亚洲AV无码精品| 亚洲精品高清在线| 久久狠狠躁免费观看2020| 亚洲日本久久一区二区va| 国产精品美女自在线观看免费| 亚洲av无码片区一区二区三区| 国产成人无码区免费网站| 91精品国产亚洲爽啪在线影院| 精精国产www视频在线观看免费| 麻豆国产入口在线观看免费 | 亚洲视频一区二区三区四区| 四虎影视免费永久在线观看| 很黄很污的网站免费| 亚洲欧美成人综合久久久|