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

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

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

    [collection]struts download Action

    Posted on 2005-12-07 17:21 BlueO2 閱讀(730) 評論(1)  編輯  收藏 所屬分類: MVC Framework
    雖然struts上傳下載文件很簡單,但是封裝一下還是好地。
    /*
    * $Id: DownloadAction.java 164530 2005-04-25 03:11:07Z niallp $
    *
    * Copyright 2004-2005 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */



    package org.apache.struts.actions;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;


    /**
    * This is an abstract base class that minimizes the amount of special coding
    * that needs to be written to download a file. All that is required to use
    * this class is to extend it and implement the <code>getStreamInfo()</code>
    * method so that it returns the relevant information for the file (or other
    * stream) to be downloaded. Optionally, the <code>getBufferSize()</code>
    * method may be overridden to customize the size of the buffer used to
    * transfer the file.
    *
    * @since Struts 1.2.6
    */

    public abstract class DownloadAction extends Action {

        /**
         * If the <code>getBufferSize()</code> method is not overridden, this is
         * the buffer size that will be used to transfer the data to the servlet
         * output stream.
         */

        protected static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

        /**
         * Returns the information on the file, or other stream, to be downloaded
         * by this action. This method must be implemented by an extending class.
         *
         * @param mapping  The ActionMapping used to select this instance.
         * @param form     The optional ActionForm bean for this request (if any).
         * @param request  The HTTP request we are processing.
         * @param response The HTTP response we are creating.
         *
         * @return The information for the file to be downloaded.
         *
         * @throws Exception if an exception occurs.
         */

        protected abstract StreamInfo getStreamInfo(ActionMapping mapping,
                ActionForm form, HttpServletRequest request,
                HttpServletResponse response)
                throws Exception;

        /**
         * Returns the size of the buffer to be used in transferring the data to
         * the servlet output stream. This method may be overridden by an extending
         * class in order to customize the buffer size.
         *
         * @return The size of the transfer buffer, in bytes.
         */

        protected int getBufferSize() {
            return DEFAULT_BUFFER_SIZE;
        }

        /**
         * Process the specified HTTP request, and create the corresponding HTTP
         * response (or forward to another web component that will create it).
         * Return an <code>ActionForward</code> instance describing where and how
         * control should be forwarded, or <code>null</code> if the response has
         * already been completed.
         *
         * @param mapping  The ActionMapping used to select this instance.
         * @param form     The optional ActionForm bean for this request (if any).
         * @param request  The HTTP request we are processing.
         * @param response The HTTP response we are creating.
         *
         * @throws Exception if an exception occurs.
         */

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

            StreamInfo info = getStreamInfo(mapping, form, request, response);
            String contentType = info.getContentType();
            InputStream stream = info.getInputStream();

            try {
                response.setContentType(contentType);
                copy(stream, response.getOutputStream());
            } finally {
                if (stream != null) {
                    stream.close();
                }
            }

            // Tell Struts that we are done with the response.
            return null;
        }

        /**
         * Copy bytes from an <code>InputStream</code> to an
         * <code>OutputStream</code>.
         *
         * @param input  The <code>InputStream</code> to read from.
         * @param output The <code>OutputStream</code> to write to.
         *
         * @return the number of bytes copied
         *
         * @throws IOException In case of an I/O problem
         */

        public int copy(InputStream input, OutputStream output)
                throws IOException {
            byte[] buffer = new byte[getBufferSize()];
            int count = 0;
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
                count += n;
            }
            return count;
        }

        /**
         * The information on a file, or other stream, to be downloaded by the
         * <code>DownloadAction</code>.
         */

        public static interface StreamInfo {

            /**
             * Returns the content type of the stream to be downloaded.
             *
             * @return The content type of the stream.
             */

            public abstract String getContentType();

            /**
             * Returns an input stream on the content to be downloaded. This stream
             * will be closed by the <code>DownloadAction</code>.
             *
             * @return The input stream for the content to be downloaded.
             */

            public abstract InputStream getInputStream() throws IOException;
        }

        /**
         * A concrete implementation of the <code>StreamInfo</code> interface which
         * simplifies the downloading of a file from the disk.
         */

        public static class FileStreamInfo implements StreamInfo {

            /**
             * The content type for this stream.
             */

            private String contentType;

            /**
             * The file to be downloaded.
             */

            private File file;

            /**
             * Constructs an instance of this class, based on the supplied
             * parameters.
             *
             * @param contentType The content type of the file.
             * @param file        The file to be downloaded.
             */

            public FileStreamInfo(String contentType, File file) {
                this.contentType = contentType;
                this.file = file;
            }

            /**
             * Returns the content type of the stream to be downloaded.
             *
             * @return The content type of the stream.
             */

            public String getContentType() {
                return this.contentType;
            }

            /**
             * Returns an input stream on the file to be downloaded. This stream
             * will be closed by the <code>DownloadAction</code>.
             *
             * @return The input stream for the file to be downloaded.
             */

            public InputStream getInputStream() throws IOException {
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                return bis;
            }
        }

        /**
         * A concrete implementation of the <code>StreamInfo</code> interface which
         * simplifies the downloading of a web application resource.
         */

        public static class ResourceStreamInfo implements StreamInfo {

            /**
             * The content type for this stream.
             */

            private String contentType;

            /**
             * The servlet context for the resource to be downloaded.
             */

            private ServletContext context;

            /**
             * The path to the resource to be downloaded.
             */

            private String path;

            /**
             * Constructs an instance of this class, based on the supplied
             * parameters.
             *
             * @param contentType The content type of the file.
             * @param context     The servlet context for the resource.
             * @param path        The path to the resource to be downloaded.
             */

            public ResourceStreamInfo(String contentType, ServletContext context,
                    String path) {
                this.contentType = contentType;
                this.context = context;
                this.path = path;
            }

            /**
             * Returns the content type of the stream to be downloaded.
             *
             * @return The content type of the stream.
             */

            public String getContentType() {
                return this.contentType;
            }

            /**
             * Returns an input stream on the resource to be downloaded. This stream
             * will be closed by the <code>DownloadAction</code>.
             *
             * @return The input stream for the resource to be downloaded.
             */

            public InputStream getInputStream() throws IOException {
                return context.getResourceAsStream(path);
            }
        }
    }

    Feedback

    # 請教[未登錄]  回復(fù)  更多評論   

    2008-08-15 17:17 by ling
    在eclipse+dreamwaver 8編程時,文件在dreamwaver中可以顯示但在eclipse中沒有顯示,為什么??請多多指教??萬分感謝,我的郵箱是 lingqiaoxu@sina.com

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     

    posts - 29, comments - 3, trackbacks - 0, articles - 0

    Copyright © BlueO2

    主站蜘蛛池模板: 黑人粗长大战亚洲女2021国产精品成人免费视频| 日韩一级免费视频| 久9热免费精品视频在线观看| 天黑黑影院在线观看视频高清免费 | 亚洲欧洲精品无码AV| 久久久久久亚洲精品| 亚洲国产精品日韩在线观看| 亚洲永久网址在线观看| 日韩精品免费一线在线观看 | 国产无限免费观看黄网站| 久久黄色免费网站| 免费看片免费播放| 久久99亚洲综合精品首页| 久久亚洲国产精品成人AV秋霞| 亚洲人成电影网站色www| 国内精品一级毛片免费看| 午夜高清免费在线观看| 亚洲av中文无码乱人伦在线咪咕| 亚洲色偷偷色噜噜狠狠99| 白白国产永久免费视频| 高潮内射免费看片| 精品久久久久久久久免费影院| 亚洲国产人成精品| 久久亚洲国产成人影院网站| 97超高清在线观看免费视频| 亚洲一区二区三区首页| aa毛片免费全部播放完整| 亚洲s色大片在线观看| 国产精品免费观看| 亚洲午夜精品一区二区| 精品人妻系列无码人妻免费视频| 亚洲精品无码专区在线在线播放| 在线观看亚洲免费| 最近中文字幕mv免费高清电影 | 女人张开腿等男人桶免费视频 | 久久成人免费电影| 2019亚洲午夜无码天堂| 性xxxxx大片免费视频| 亚洲人成在线免费观看| 99久久久国产精品免费无卡顿| 91丁香亚洲综合社区|