`
高军威
  • 浏览: 175625 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

上传进度条显示

阅读更多
1.重写
package com.upload.util;

   
    /**
     * $Id: JakartaMultiPartRequest.java 932009 2010-04-08 17:01:16Z lukaszlenart $
     *
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you 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.
     */
    /*Struts 文件上传拦截器接口
     * 
     * 1.拦截所有文件上传的请求
     * 2.一般只需要重写parseRequest这个方法
     * 3.目前实现文件上传进度条
     * 
     * */

    import com.opensymphony.xwork2.inject.Inject;
    import com.opensymphony.xwork2.util.logging.Logger;
    import com.opensymphony.xwork2.util.logging.LoggerFactory;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
    import org.apache.commons.fileupload.RequestContext;
    import org.apache.commons.fileupload.disk.DiskFileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.multipart.MultiPartRequest;

    import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.List;
import java.util.Map;

    /***
     * Multipart form data request adapter for Jakarta Commons Fileupload package.
     */
    public class myMultiPartRequest implements MultiPartRequest {
        
        static final Logger LOG = LoggerFactory.getLogger(MultiPartRequest.class);
        
        // maps parameter name -> List of FileItem objects
        protected Map<String,List<FileItem>> files = new HashMap<String,List<FileItem>>();

        // maps parameter name -> List of param values
        protected Map<String,List<String>> params = new HashMap<String,List<String>>();

        // any errors while processing this request
        protected List<String> errors = new ArrayList<String>();
        
        protected long maxSize;

        @Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
        public void setMaxSize(String maxSize) {
            this.maxSize = Long.parseLong(maxSize);
        }

        /***
         * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
         * multipart classes (see class description).
         *
         * @param saveDir        the directory to save off the file
         * @param request the request containing the multipart
         * @throws java.io.IOException  is thrown if encoding fails.
         */
        public void parse(HttpServletRequest request, String saveDir) throws IOException {
            try {
                processUpload(request, saveDir);
            } catch (FileUploadException e) {
                LOG.warn("Unable to parse request", e);
                errors.add(e.getMessage());
            }
        }

        private void processUpload(HttpServletRequest request, String saveDir) throws FileUploadException, UnsupportedEncodingException {
            for (FileItem item : parseRequest(request, saveDir)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found item " + item.getFieldName());
                }
                if (item.isFormField()) {
                    processNormalFormField(item, request.getCharacterEncoding());
                } else {
                    processFileField(item);
                }
            }
        }

        private void processFileField(FileItem item) {
            LOG.debug("Item is a file upload");

            // Skip file uploads that don't have a file name - meaning that no file was selected.
            if (item.getName() == null || item.getName().trim().length() < 1) {
                LOG.debug("No file has been uploaded for the field: " + item.getFieldName());
                return;
            }

            List<FileItem> values;
            if (files.get(item.getFieldName()) != null) {
                values = files.get(item.getFieldName());
            } else {
                values = new ArrayList<FileItem>();
            }

            values.add(item);
            files.put(item.getFieldName(), values);
        }

        private void processNormalFormField(FileItem item, String charset) throws UnsupportedEncodingException {
            LOG.debug("Item is a normal form field");
            List<String> values;
            if (params.get(item.getFieldName()) != null) {
                values = params.get(item.getFieldName());
            } else {
                values = new ArrayList<String>();
            }

            // note: see http://jira.opensymphony.com/browse/WW-633
            // basically, in some cases the charset may be null, so
            // we're just going to try to "other" method (no idea if this
            // will work)
            if (charset != null) {
                values.add(item.getString(charset));
            } else {
                values.add(item.getString());
            }
            params.put(item.getFieldName(), values);
        }

        private List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
        final HttpSession session = servletRequest.getSession();
            DiskFileItemFactory fac = createDiskFileItemFactory(saveDir);
            ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setProgressListener(new ProgressListener() {
            public void update(long pBytesRead, long pContentLength, int pItems) {
                    int percent = (int) (((float)pBytesRead / (float)pContentLength) * 100);
                    session.setAttribute("percent", percent);
                }
            });
            upload.setSizeMax(maxSize);
            return upload.parseRequest(createRequestContext(servletRequest));
        }

        private DiskFileItemFactory createDiskFileItemFactory(String saveDir) {
            DiskFileItemFactory fac = new DiskFileItemFactory();
            // Make sure that the data is written to file
            fac.setSizeThreshold(0);
            if (saveDir != null) {
                fac.setRepository(new File(saveDir));
            }
            return fac;
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFileParameterNames()
         */
        public Enumeration<String> getFileParameterNames() {
            return Collections.enumeration(files.keySet());
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getContentType(java.lang.String)
         */
        public String[] getContentType(String fieldName) {
            List<FileItem> items = files.get(fieldName);

            if (items == null) {
                return null;
            }

            List<String> contentTypes = new ArrayList<String>(items.size());
            for (FileItem fileItem : items) {
                contentTypes.add(fileItem.getContentType());
            }

            return contentTypes.toArray(new String[contentTypes.size()]);
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFile(java.lang.String)
         */
        public File[] getFile(String fieldName) {
            List<FileItem> items = files.get(fieldName);

            if (items == null) {
                return null;
            }

            List<File> fileList = new ArrayList<File>(items.size());
            for (FileItem fileItem : items) {
                fileList.add(((DiskFileItem) fileItem).getStoreLocation());
            }

            return fileList.toArray(new File[fileList.size()]);
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFileNames(java.lang.String)
         */
        public String[] getFileNames(String fieldName) {
            List<FileItem> items = files.get(fieldName);

            if (items == null) {
                return null;
            }

            List<String> fileNames = new ArrayList<String>(items.size());
            for (FileItem fileItem : items) {
                fileNames.add(getCanonicalName(fileItem.getName()));
            }

            return fileNames.toArray(new String[fileNames.size()]);
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFilesystemName(java.lang.String)
         */
        public String[] getFilesystemName(String fieldName) {
            List<FileItem> items = files.get(fieldName);

            if (items == null) {
                return null;
            }

            List<String> fileNames = new ArrayList<String>(items.size());
            for (FileItem fileItem : items) {
                fileNames.add(((DiskFileItem) fileItem).getStoreLocation().getName());
            }

            return fileNames.toArray(new String[fileNames.size()]);
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameter(java.lang.String)
         */
        public String getParameter(String name) {
            List<String> v = params.get(name);
            if (v != null && v.size() > 0) {
                return v.get(0);
            }

            return null;
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterNames()
         */
        public Enumeration<String> getParameterNames() {
            return Collections.enumeration(params.keySet());
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterValues(java.lang.String)
         */
        public String[] getParameterValues(String name) {
            List<String> v = params.get(name);
            if (v != null && v.size() > 0) {
                return v.toArray(new String[v.size()]);
            }

            return null;
        }

        /** (non-Javadoc)
         * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getErrors()
         */
        public List getErrors() {
            return errors;
        }

        /***
         * Returns the canonical name of the given file.
         *
         * @param filename  the given file
         * @return the canonical name of the given file
         */
        private String getCanonicalName(String filename) {
            int forwardSlash = filename.lastIndexOf("/");
            int backwardSlash = filename.lastIndexOf("\\");
            if (forwardSlash != -1 && forwardSlash > backwardSlash) {
                filename = filename.substring(forwardSlash + 1, filename.length());
            } else if (backwardSlash != -1 && backwardSlash >= forwardSlash) {
                filename = filename.substring(backwardSlash + 1, filename.length());
            }

            return filename;
        }

        /***
         * Creates a RequestContext needed by Jakarta Commons Upload.
         *
         * @param req  the request.
         * @return a new request context.
         */
        private RequestContext createRequestContext(final HttpServletRequest req) {
            return new RequestContext() {
                public String getCharacterEncoding() {
                    return req.getCharacterEncoding();
                }

                public String getContentType() {
                    return req.getContentType();
                }

                public int getContentLength() {
                    return req.getContentLength();
                }

                public InputStream getInputStream() throws IOException {
                    InputStream in = req.getInputStream();
                    if (in == null) {
                        throw new IOException("Missing content in the request");
                    }
                    return req.getInputStream();
                }
            };
        }

    }


2.得到当前进度值
 public String getPercentage()
    {
        HttpSession session = getRequest().getSession();
        int status = (Integer) session.getAttribute("percent");
        setJsonStr(new SwikeResult(status + "").toJson());
        return SUCCESS;
    }


3.前台写个定时器 没过 1秒取一次值,在动态修改dom元素值就Ok
分享到:
评论

相关推荐

    基于VB实现的商场管理系统设计(源代码+系统).zip

    【作品名称】:基于VB实现的商场管理系统设计(源代码+系统) 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。

    数据更新至2020年主要发电企业火电机组分容量等级发电装机容量情况.xls

    数据来源:中国电力统计NJ-2021版

    数据更新至2020年电网建设 本年开工规模.xls

    数据来源:中国电力统计NJ-2021版

    基于C# WinForm框架开发的图书管理系统源码+sql文件.zip

    基于C# WinForm框架开发的图书管理系统源码+sql文件.zip基于C# WinForm框架开发的图书管理系统源码+sql文件.zip基于C# WinForm框架开发的图书管理系统源码+sql文件.zip基于C# WinForm框架开发的图书管理系统源码+sql文件.zip基于C# WinForm框架开发的图书管理系统源码+sql文件.zip

    毕业设计(论文)-基于Android系统的人事管理系统设计与实现(48页).doc

    毕业设计(论文)-基于Android系统的人事管理系统设计与实现(48页).doc

    pentair 5800 SXT软水机说明书

    pentair 5800 SXT软水机说明书

    (更新至2022年)各地区乡村分性别的15岁及以上文盲人口.xls

    数据来源:中国人口与就业统计NJ-2023版

    node-v12.11.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v9.6.1-sunos-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    (更新至2022年)全国镇分年龄、性别的人口数.xls

    数据来源:中国人口与就业统计NJ-2023版

    (更新至2022年)消费物价指数 (2019年10月-2020年9月=100).xls

    数据来源:中国人口与就业统计NJ-2023版

    node-v10.23.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v7.6.0-x86.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v11.4.0-linux-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    小区物业管理系统设计与实现.docx

    小区物业管理系统设计与实现.docx

    律师事务所办公管理系统设计及实现.doc

    律师事务所办公管理系统设计及实现.doc

    基于Qt与Android的KTV管理系统设计与实现毕业论文(25页).doc

    基于Qt与Android的KTV管理系统设计与实现毕业论文(25页).doc

    消费价格指数(2010年=100).xls

    数据来源:中国劳动统计NJ-2023版

    华为 OD 机考攻略-加强版

    附件是华为 OD 机考攻略_加强版,文件绿色安全,请大家放心下载,仅供交流学习使用,无任何商业目的!

    node-v12.13.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

Global site tag (gtag.js) - Google Analytics