Package org.nutz.filepool

Examples of org.nutz.filepool.FilePool


    /*
     * 初始化一些临时变量
     */
    int bufferSize = context.getBufferSize();
    String charset = context.getCharset();
    FilePool tmps = context.getFilePool();
    int maxFileSize = context.getMaxFileSize();

    /*
     * 创建进度对象
     */
    UploadInfo info = Uploads.createInfo(req);
    if (log.isDebugEnabled())
      log.debug("info created");
    /*
     * 创建参数表
     */
    NutMap params = Uploads.createParamsMap(req);
    if (log.isDebugEnabled())
      log.debugf("Params map created - %s params", params.size());
    /*
     * 解析边界
     */
    String firstBoundary = "--" + Http.multipart.getBoundary(req.getContentType());
    RemountBytes firstBoundaryBytes = RemountBytes.create(firstBoundary);
    String itemEndl = "\r\n--" + Http.multipart.getBoundary(req.getContentType());
    RemountBytes itemEndlBytes = RemountBytes.create(itemEndl);
    RemountBytes nameEndlBytes = RemountBytes.create("\r\n\r\n");

    if (Http.multipart.getBoundary(req.getContentType()) == null) {
      if (log.isInfoEnabled())
        log.info("boundary no found!!");
      return params;
    }

    if (log.isDebugEnabled())
      log.debug("boundary: " + itemEndl);

    /*
     * 准备缓冲环,并跳过开始标记
     */
    MarkMode mm;
    BufferRing br;
    try {
      ServletInputStream ins = req.getInputStream();
      // 构建 3 个环节点的缓冲环
      br = new BufferRing(ins, 3, bufferSize);
      // 初始加载
      info.current = br.load();
      // 跳过开始的标记
      mm = br.mark(firstBoundaryBytes);
      // 这是不可能的,应该立即退出
      if (mm != MarkMode.FOUND) {
        if (log.isWarnEnabled())
          log.warnf("Fail to find the firstBoundary (%s) in stream, quit!", firstBoundary);
        return params;
      }
      br.skipMark();
      if (log.isDebugEnabled())
        log.debug("skip first boundary");
    }
    catch (IOException e) {
      throw Lang.wrapThrow(e);
    }

    /**
     * ========================================================<br>
     * 进入循环
     */
    if (log.isDebugEnabled())
      log.debug("Reading...");
    try {
      FieldMeta meta;
      do {
        info.current = br.load();
        // 标记项目头
        mm = br.mark(nameEndlBytes);
        String s = br.dumpAsString(charset);

        // 肯定碰到了 "--\r\n", 这标志着整个流结束了
        if ("--".equals(s) || MarkMode.STREAM_END == mm) {
          break;
        }
        // 找到头的结束标志
        else if (MarkMode.FOUND == mm) {
          meta = new FieldMeta(s);
        }
        // 这是不可能的,抛错
        else {
          throw new UploadInvalidFormatException("Fail to found nameEnd!");
        }
        if(log.isDebugEnabled())
          log.debugf("Upload File info: FilePath=[%s],fieldName=[%s]",meta.getFileLocalPath(),meta.getName());
        // 作为文件读取
        if (meta.isFile()) {
          if (log.isDebugEnabled())
            log.debugf("Upload Info: name=%s,content_type=%s", meta.getFileLocalName(),meta.getContentType());
          // 检查是否通过文件名过滤
          if (!context.isNameAccepted(meta.getFileLocalName())) {
            throw new UploadUnsupportedFileNameException(meta);
          }
          // 检查是否通过文件类型过滤
          if (!context.isContentTypeAccepted(meta.getContentType())) {
            throw new UploadUnsupportedFileTypeException(meta);
          }

          // 上传的是一个空文件
          if ("\"\"".equals(meta.getName()) || Strings.isBlank(meta.getFileLocalPath())) {
            do {
              info.current = br.load();
              mm = br.mark(itemEndlBytes);
              assertStreamNotEnd(mm);
              br.skipMark();
            } while (mm == MarkMode.NOT_FOUND);
          }
          // 保存临时文件
          else {
            File tmp = tmps.createFile(meta.getFileExtension());
            OutputStream ops = null;
            try {
              ops = new BufferedOutputStreamnew FileOutputStream(tmp),
                              bufferSize * 2);
              // 需要限制文件大小
View Full Code Here


        /*
         * 初始化一些临时变量
         */
        int bufferSize = context.getBufferSize();
        String charset = context.getCharset();
        FilePool tmps = context.getFilePool();
        int maxFileSize = context.getMaxFileSize();

        /*
         * 创建进度对象
         */
        UploadInfo info = Uploads.createInfo(req);
        if (log.isDebugEnabled())
            log.debug("info created");
        /*
         * 创建参数表
         */
        NutMap params = Uploads.createParamsMap(req);
        if (log.isDebugEnabled())
            log.debugf("Params map created - %s params", params.size());
        /*
         * 解析边界
         */
        String firstBoundary = "--" + Http.multipart.getBoundary(req.getContentType());
        RemountBytes firstBoundaryBytes = RemountBytes.create(firstBoundary);
        String itemEndl = "\r\n--" + Http.multipart.getBoundary(req.getContentType());
        RemountBytes itemEndlBytes = RemountBytes.create(itemEndl);
        RemountBytes nameEndlBytes = RemountBytes.create("\r\n\r\n");

        if (Http.multipart.getBoundary(req.getContentType()) == null) {
            if (log.isInfoEnabled())
                log.info("boundary no found!!");
            return params;
        }

        if (log.isDebugEnabled())
            log.debug("boundary: " + itemEndl);

        /*
         * 准备缓冲环,并跳过开始标记
         */
        MarkMode mm;
        BufferRing br;
        try {
            ServletInputStream ins = req.getInputStream();
            // 构建 3 个环节点的缓冲环
            br = new BufferRing(ins, 3, bufferSize);
            // 初始加载
            info.current = br.load();
            // 跳过开始的标记
            mm = br.mark(firstBoundaryBytes);
            // 这是不可能的,应该立即退出
            if (mm != MarkMode.FOUND) {
                if (log.isWarnEnabled())
                    log.warnf("Fail to find the firstBoundary (%s) in stream, quit!", firstBoundary);
                return params;
            }
            br.skipMark();
            if (log.isDebugEnabled())
                log.debug("skip first boundary");
        }
        catch (IOException e) {
            throw Lang.wrapThrow(e);
        }

        /**
         * ========================================================<br>
         * 进入循环
         */
        if (log.isDebugEnabled())
            log.debug("Reading...");
        try {
            FieldMeta meta;
            do {
                info.current = br.load();
                // 标记项目头
                mm = br.mark(nameEndlBytes);
                String s = br.dumpAsString(charset);

                // 肯定碰到了 "--\r\n", 这标志着整个流结束了
                if ("--".equals(s) || MarkMode.STREAM_END == mm) {
                    break;
                }
                // 找到头的结束标志
                else if (MarkMode.FOUND == mm) {
                    meta = new FieldMeta(s);
                }
                // 这是不可能的,抛错
                else {
                    throw new UploadInvalidFormatException("Fail to found nameEnd!");
                }
                if(log.isDebugEnabled())
                    log.debugf("Upload File info: FilePath=[%s],fieldName=[%s]",meta.getFileLocalPath(),meta.getName());
                // 作为文件读取
                if (meta.isFile()) {
                    if (log.isDebugEnabled())
                        log.debugf("Upload Info: name=%s,content_type=%s", meta.getFileLocalName(),meta.getContentType());
                    // 检查是否通过文件名过滤
                    if (!context.isNameAccepted(meta.getFileLocalName())) {
                        throw new UploadUnsupportedFileNameException(meta);
                    }
                    // 检查是否通过文件类型过滤
                    if (!context.isContentTypeAccepted(meta.getContentType())) {
                        throw new UploadUnsupportedFileTypeException(meta);
                    }

                    // 上传的是一个空文件
                    if ("\"\"".equals(meta.getName()) || Strings.isBlank(meta.getFileLocalPath())) {
                        do {
                            info.current = br.load();
                            mm = br.mark(itemEndlBytes);
                            assertStreamNotEnd(mm);
                            br.skipMark();
                        } while (mm == MarkMode.NOT_FOUND);
                    }
                    // 保存临时文件
                    else {
                        File tmp = tmps.createFile(meta.getFileExtension());
                        OutputStream ops = null;
                        try {
                            ops = new BufferedOutputStream(    new FileOutputStream(tmp),
                                                            bufferSize * 2);
                            // 需要限制文件大小
View Full Code Here

            UploadUnsupportedFileTypeException {
       
        int bufferSize = context.getBufferSize();
        int size = req.getContentLength();
        int maxSize = context.getMaxFileSize();
        FilePool tmps = context.getFilePool();
       
        FieldMeta meta = new FieldMeta("name=\"filedata\"; filename=\"nutz.jpg\""); //默认字段名,文件名
       
        //检查大小
        if (maxSize > 0 && size > context.getMaxFileSize()) {
            throw new UploadOutOfSizeException(meta);
        }
       
        //获取文件名
        String disposition = req.getHeader("Content-Disposition");
        if (disposition != null && disposition.startsWith("attachment;")) {
            meta = new FieldMeta(disposition.substring("attachment;".length()).trim());
        } else {
            if (log.isInfoEnabled())
                log.info("Content-Disposition no found, using default fieldname=filedata, filename=nutz.jpg");
        }
       
        if(log.isDebugEnabled())
            log.debugf("Upload File info: FilePath=[%s],fieldName=[%s]",meta.getFileLocalPath(),meta.getName());
       
        // 检查是否通过文件名过滤
        if (!context.isNameAccepted(meta.getFileLocalName())) {
            throw new UploadUnsupportedFileNameException(meta);
        }
        // 检查是否通过文件类型过滤 TODO 不可检查吗?
        //if (!context.isContentTypeAccepted(meta.getContentType())) {
        //    throw new UploadUnsupportedFileTypeException(meta);
        //}
       
        //开始读取数据
        File tmp = tmps.createFile(meta.getFileExtension());
        OutputStream ops = null;
        try {
            ops = new BufferedOutputStream(    new FileOutputStream(tmp),bufferSize * 2);
            Streams.writeAndClose(ops, req.getInputStream());
           
View Full Code Here

        /*
         * 初始化一些临时变量
         */
        int bufferSize = context.getBufferSize();
        String charset = context.getCharset();
        FilePool tmps = context.getFilePool();
        int maxFileSize = context.getMaxFileSize();

        /*
         * 创建进度对象
         */
        UploadInfo info = Uploads.createInfo(req);
        if (log.isDebugEnabled())
            log.debug("info created");
        /*
         * 创建参数表
         */
        NutMap params = Uploads.createParamsMap(req);
        if (log.isDebugEnabled())
            log.debugf("Params map created - %s params", params.size());
        /*
         * 解析边界
         */
        String firstBoundary = "--" + Http.multipart.getBoundary(req.getContentType());
        RemountBytes firstBoundaryBytes = RemountBytes.create(firstBoundary);
        String itemEndl = "\r\n--" + Http.multipart.getBoundary(req.getContentType());
        RemountBytes itemEndlBytes = RemountBytes.create(itemEndl);
        RemountBytes nameEndlBytes = RemountBytes.create("\r\n\r\n");

        if (Http.multipart.getBoundary(req.getContentType()) == null) {
            if (log.isInfoEnabled())
                log.info("boundary no found!!");
            return params;
        }

        if (log.isDebugEnabled())
            log.debug("boundary: " + itemEndl);

        /*
         * 准备缓冲环,并跳过开始标记
         */
        MarkMode mm;
        BufferRing br;
        try {
            ServletInputStream ins = req.getInputStream();
            // 构建 3 个环节点的缓冲环
            br = new BufferRing(ins, 3, bufferSize);
            // 初始加载
            info.current = br.load();
            // 跳过开始的标记
            mm = br.mark(firstBoundaryBytes);
            // 这是不可能的,应该立即退出
            if (mm != MarkMode.FOUND) {
                if (log.isWarnEnabled())
                    log.warnf("Fail to find the firstBoundary (%s) in stream, quit!", firstBoundary);
                return params;
            }
            br.skipMark();
            if (log.isDebugEnabled())
                log.debug("skip first boundary");
        }
        catch (IOException e) {
            throw Lang.wrapThrow(e);
        }

        /**
         * ========================================================<br>
         * 进入循环
         */
        if (log.isDebugEnabled())
            log.debug("Reading...");
        try {
            FieldMeta meta;
            do {
                info.current = br.load();
                // 标记项目头
                mm = br.mark(nameEndlBytes);
                String s = br.dumpAsString(charset);

                // 肯定碰到了 "--\r\n", 这标志着整个流结束了
                if ("--".equals(s) || MarkMode.STREAM_END == mm) {
                    break;
                }
                // 找到头的结束标志
                else if (MarkMode.FOUND == mm) {
                    meta = new FieldMeta(s);
                }
                // 这是不可能的,抛错
                else {
                    throw new UploadInvalidFormatException("Fail to found nameEnd!");
                }
                if(log.isDebugEnabled())
                    log.debugf("Upload File info: FilePath=[%s],fieldName=[%s]",meta.getFileLocalPath(),meta.getName());
                // 作为文件读取
                if (meta.isFile()) {
                    if (log.isDebugEnabled())
                        log.debugf("Upload Info: name=%s,content_type=%s", meta.getFileLocalName(),meta.getContentType());
                    // 检查是否通过文件名过滤
                    if (!context.isNameAccepted(meta.getFileLocalName())) {
                        throw new UploadUnsupportedFileNameException(meta);
                    }
                    // 检查是否通过文件类型过滤
                    if (!context.isContentTypeAccepted(meta.getContentType())) {
                        throw new UploadUnsupportedFileTypeException(meta);
                    }

                    // 上传的是一个空文件
                    if ("\"\"".equals(meta.getName()) || Strings.isBlank(meta.getFileLocalPath())) {
                        do {
                            info.current = br.load();
                            mm = br.mark(itemEndlBytes);
                            assertStreamNotEnd(mm);
                            br.skipMark();
                        } while (mm == MarkMode.NOT_FOUND);
                    }
                    // 保存临时文件
                    else {
                        File tmp = tmps.createFile(meta.getFileExtension());
                        OutputStream ops = null;
                        try {
                            ops = new BufferedOutputStream(    new FileOutputStream(tmp),
                                                            bufferSize * 2);
                            // 需要限制文件大小
View Full Code Here

TOP

Related Classes of org.nutz.filepool.FilePool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.