• 文件上传限制文件类型


    public class FileUploadManager {
        /** 允许上传的扩展名*/  
        public static String [] extensionPermit = {"txt", "xls", "zip"}; 
    
        public FileUploadManager(User user, HttpServletRequest request) throws Exception {
            super();
            session = request.getSession();
            DiskFileItemFactory factory = new DiskFileItemFactory();
            upload = new ServletFileUpload(factory);
            SystemManager systemManager = ComponentFactory.getSystemManager();
            int docSize = Integer.parseInt(systemManager.getSystemConfig(user.getCompanyID(), "DOCUMENT_SIZE_LIMIT").getValue());
            int maxFileSize = 1024 * 1024 * docSize;
            upload.setFileSizeMax(maxFileSize); // 设置单个文件的最大上传大小
            upload.setSizeMax(maxFileSize + 500); // 设置整个request的最大大小
            upload.setProgressListener(new FileUploadListener(request));
            extensionPermit = systemManager.getSystemConfig(user.getCompanyID(), "DOCUMENT_EXT_LIMIT").getValue().split(",");//设置文件类型
            files = upload.parseRequest(request);
        }
    
    
    class QCServletFileUpload extends ServletFileUpload {
        private Map textHash = new HashMap();//表单数据
        private int fileItemCount = 0;
    
        // 重载parseRequest方法
        public List parseRequest(HttpServletRequest request) throws FileUploadException {
            List items = new ArrayList();//文件
            try {
                FileItemIterator iter = getItemIterator(request);
                FileItemFactory fac = getFileItemFactory();
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();
                    FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(), item.isFormField(), item.getName());
                    Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
                    //fileItem.getOutputStream().close();
                    if (fileItem.isFormField()) { // 处理form field
                        textHash.put(fileItem.getFieldName(), fileItem);
                    } else if (fileItem.getName() != null && fileItem.getName().length() > 0) {// 处理file field
                        String fileExtension = FilenameUtils.getExtension(fileItem.getName());
                        if(!ArrayUtils.contains(FileUploadManager.extensionPermit, fileExtension)){
                            fileItemCount++;
                            items.add(fileItem);
                        }else{
                            throw new FileUploadException("文件" + fileItem.getName() + "类型不支持.");
                        }
                        
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new FileUploadException(e.getMessage(), e);
            }
            return items;
        }
    
    }
  • 相关阅读:
    软件工程第一次实验——软件开发文档与工具的安装与使用
    软件工程第四次作业——ATM管理系统
    软件工程第三次作业——举例分析流程图与活动图的区别与联系
    软件工程第二次作业——小学四则运算题目生成程序
    mac终端进入mysql
    计算机网络
    类和对象(未完成)
    html_css
    Mac下使用C语言生成和使用动态链接库
    模运算
  • 原文地址:https://www.cnblogs.com/firstdream/p/7376333.html
Copyright © 2020-2023  润新知