zoukankan      html  css  js  c++  java
  • 文件大小限制

    上传的文件不能太大,因此要对文件大小做一定的限制。

    限制单个文件的大小:

    1 sfu.setFileSizeMax(1024 * 10);//10K

    限制所有文件的总大小:

    1 sfu.setSizeMax(1024 * 30);

    具体示例代码:

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //设置字符编码
            //能控制文件名字的中文编码,但是不能控制普通字段的中文乱码
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            //设置返回的内容类型
            response.setContentType("text/html;charset=UTF-8");
            //创建接收文件的工厂类
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //创建文件解析对象
            ServletFileUpload sfu = new ServletFileUpload(factory);
            //设置解析request请求的编码方式
    //        sfu.setHeaderEncoding("UTF-8");
            //设置上传单个文件的大小,以byte为单位
            sfu.setFileSizeMax(1024 * 10);//10K
            //设置所有文件的总大小
            sfu.setSizeMax(1024 * 30);
            try {
                //解析request获得表单中的每一文件项(包含普通文本域)
                List<FileItem> fiList = sfu.parseRequest(request);
                //遍历每一个文本项
                for(FileItem fi : fiList){
                    //获得原始文件名
                    String originalFilename = fi.getName();
                    //获得字段名
                    String field = fi.getFieldName();
                    //是否是普通字段
                    //如果true,代表普通字段(a simple form field)
                    //如果false,代表文件字段
                    boolean isFormField = fi.isFormField();
                    //判断是否是文件字段
                    if(!isFormField){
                        if(field != null && !"".equals(field) ){
                            //生成文件名UUID,使文件名不会重复
                            String uuid = UUID.randomUUID().toString();
                            //获得原始文件的后缀名
                            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                            //设置要上传的目录,如果是隐私的文件可以放在WEB-INF下来提高安全性
                            String uploadPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
                            //创建文件对象
                            File file = new File(uploadPath,uuid+suffix);//参数:文件目录,文件名
                            //把文件写入硬盘
                            fi.write(file);
                        }
                    } else {
                        System.out.println(fi.getString());
                        //手动转换编码
                        String content = fi.getString();
                        content = new String(content.getBytes("ISO8859-1"),"UTF-8");
                        System.out.println(content);
                    }
                }
                //回写结果
                response.getWriter().print("文件上传成功。");
            } catch (Exception e) {
                //捕获单个文件过大的异常
                if(e instanceof FileSizeLimitExceededException){
                    response.getWriter().write("上传失败,单个文件过大。");
                }
                //捕获文件总大小的异常
                if(e instanceof SizeLimitExceededException){
                    response.getWriter().write("文件总大小不能超过30KB");
                }
                e.printStackTrace();
            }
        }
  • 相关阅读:
    this 关键字
    Win10 删除“此电脑-文件夹”中的七个快捷方式
    ESLint 配置(三)Vue 项目
    ESLint 配置(二)Standard 和 Prettier
    ESLint 配置(一)基础配置
    简易FTP
    选课系统
    ATM+购物车
    ATM+购物车导图
    函数作业
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/7647188.html
Copyright © 2011-2022 走看看