Examples of SmartAccessFile


Examples of org.fto.jthink.io.SmartAccessFile

 
  /**
   * 上传文件. 此类型被实例化后,还必须调用此方法才能正确上传文件
   */
  public void upload(){
    SmartAccessFile raf = null;
//    long time = System.currentTimeMillis();
    try{
     
      /* 创建临时文件 */
      tempFile = java.io.File.createTempFile("httpUpload", ".tmp");
      raf = new SmartAccessFile(tempFile, "rw");
      /* 返回总字节数量 */
      totalBytes = request.getServletRequest().getContentLength();
//      logger.debug("流失时间: "+(System.currentTimeMillis()-time));
      if (maxContentLength > (long)0   && totalBytes > maxContentLength){
        request.getServletRequest().getInputStream().close();
        throw new JThinkRuntimeException("Total request content size exceeded.")//请求的内容太大,不允许上传文件
      }
     
      /* 读入所有字节数据,并保存到临时文件 */
      InputStream is = request.getServletRequest().getInputStream();
      int readBytes = 1024*256;
      if(totalBytes>1024*256 && totalBytes<=1024*512){
        readBytes = 1024*512;
      }else if(totalBytes>1024*512 && totalBytes<=1048576){
        readBytes = 1048576;
      }else if(totalBytes>1048576 && totalBytes<=1048576*5){
        readBytes = 1048576*2;
      }else if(totalBytes>=1048576*5){
        readBytes = 1048576*5;
      }

      int tmpReadBytes = 0;
      byte[] bs = new byte[readBytes];
      for(int currBytes=0;currBytes<totalBytes;){
        int count = is.read(bs, tmpReadBytes, readBytes-tmpReadBytes);
        currBytes += count;
        tmpReadBytes += count;
        if(tmpReadBytes==readBytes || currBytes>=totalBytes){
          raf.write(bs, 0, tmpReadBytes);
          tmpReadBytes = 0;
        }
      }

      /* 查找请求数据的开始索引位置和请求数据块间的分隔串 */
      raf.seek(0);
      int v = raf.read();
      boundary = "";
      while(v!=13 && raf.getFilePointer() < totalBytes){
        boundary += (char)v;
        v = raf.read();
      }
     
      /* 如果没有任何请求信息, 直接返回 */
      if(raf.getFilePointer()==1 || raf.getFilePointer()>=raf.length()){
        return;
      }
      while(true){
        if (raf.getFilePointer() >= totalBytes){
          break;
        }
        /* 返回字段头信息 */
        String dataHeader = getDataHeader(raf);
        boolean isFile = dataHeader.indexOf("filename") > 0;
        String fieldName = getDataFieldValue(dataHeader, "name");
        if (isFile) {
          filePathName = getDataFieldValue(dataHeader, "filename");
          fileName = preName + getFileName(filePathName);
          fileExt = getFileExt(fileName);
          contentType = getContentType(dataHeader);
          contentDisp = getContentDisp(dataHeader);
          typeMIME = getTypeMIME(contentType);
          subTypeMIME = getSubTypeMIME(contentType);
        }

        /* 处理数据片段 */
        getDataSection(raf);
       
        /* 判断文件是否被允许上传 */
        if (isFile && fileName.length() > 0) {
          if (deniedFilesList!=null && deniedFilesList.contains(fileExt)){
            throw new JThinkRuntimeException(
                "The extension of the file is denied to be uploaded.");//不允许上传此种类型的文件
          }
          if (allowedFilesList!=null && !allowedFilesList.contains(fileExt)){
            throw new JThinkRuntimeException(
                "The extension of the file is not allowed to be uploaded.");//不允许上传此种类型的文件
          }
         
          if (maxFileSize > (long) && (long) ((endData - startData) + 1) > maxFileSize){
            throw new JThinkRuntimeException(String.valueOf(
                (new StringBuffer("Size exceeded for this file : ")).append(fileName)));//文件太大,不允许上传
          }
        }
       
        if (isFile) {
          /* 生成被上传的单个临时文件 */
          org.fto.jthink.j2ee.web.fileload.File newFile = new org.fto.jthink.j2ee.web.fileload.File();
          newFile.setParent(this);
          newFile.setFieldName(fieldName);
          newFile.setFileName(fileName);
          newFile.setFileExt(fileExt);
          newFile.setFilePathName(filePathName);
          newFile.setIsMissing(filePathName.length() == 0);
          newFile.setContentType(contentType);
          newFile.setContentDisp(contentDisp);
          newFile.setTypeMIME(typeMIME);
          newFile.setSubTypeMIME(subTypeMIME);
          if (contentType.indexOf("application/x-macbinary") > 0){
            startData = startData + 128;
          }
          newFile.setSize((endData - startData));
          newFile.setStartData(startData);
          newFile.setEndData(endData);
          files.add(newFile);
         
        } else {
          /* 生成非文件的请求信息数据 */
          long tmpPointer = raf.getFilePointer();
          raf.seek(startData);
          byte[] values = new byte[(endData - startData)];
          raf.read(values);
          String value;
          if(encode==null){
            value = new String(values);
          }else{
            value = new String(values, encode);
          }
          request.putParameter(fieldName, value);
          raf.seek(tmpPointer);
        }
       
        raf.skipBytes(1);
        char c = (char)raf.read();
        /* 如果遇到'-'字符,跳出循环 */
        if (c == '-'){
          raf.skipBytes(-1);
          break;
        }
      }
     
     
View Full Code Here

Examples of org.fto.jthink.io.SmartAccessFile

  /**
   *方法测试
   */
  public void testindexOf() {
    System.out.println("\n[正在测试方法: SmartAccessFile.indexOf()...]");
    SmartAccessFile saf = null;
    try {
      saf = new SmartAccessFile("SmartAccessFile_testIndexOf.txt", "rw");
      byte[] safs = new byte[(int)saf.length()];
      saf.read(safs);
      this.printBytes("safs:", safs);
     
      saf.seek(0);
      byte[] bs = "pqrstuvwxyz".getBytes();
      long index = saf.indexOf(bs, 0, bs.length);
      System.out.println("saf.indexOf(bs) 1:"+index);
      if(index!=-1){
        super.fail();
      }
      saf.seek(0);
      bs = "0123456789".getBytes();
      index = saf.indexOf(bs, 0, bs.length);
      System.out.println("saf.indexOf(bs) 2:"+index);
      if(index!=3){
        super.fail();
      }
      saf.seek(0);
      bs = "试索引".getBytes("gb2312");
      index = saf.indexOf(bs, 0, bs.length);
      this.printBytes("试索引:", bs);
     
      System.out.println("saf.indexOf(bs) 3:"+index);
      if(index!=17){
        super.fail();
View Full Code Here

Examples of org.fto.jthink.io.SmartAccessFile

  /**
   *方法测试
   */
  public void testIsEOF() {
    System.out.println("\n[正在测试方法: SmartAccessFile.isEOF()...]");
    SmartAccessFile saf = null;
    try {
      saf = new SmartAccessFile(testedfileRW, "rw");
      saf.insert("Test inEOF()".getBytes());
      saf.seek(saf.length());
      assertTrue("是文件尾:", saf.isEOF());
      saf.seek(1);
      assertTrue("不是文件尾:", !saf.isEOF());
     
     
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
View Full Code Here

Examples of org.fto.jthink.io.SmartAccessFile

  /**
   *方法测试
   */
  public void testInsert() {
    System.out.println("\n[正在测试方法: SmartAccessFile.insert()...]");
    SmartAccessFile safrw = null;
    SmartAccessFile safr = null;
    SmartAccessFile safa = null;
   
    /* 测试"rw"模式 */
    try {
      safrw = new SmartAccessFile(testedfileRW, "rw");
      byte[] bs = null;
      safrw.seek(safrw.length());
      long beginLen = safrw.length();
      safrw.insert("测试直接调用RandomAccessFile的write方法!".getBytes());
      bs = new byte[(int)safrw.length()-(int)beginLen];
      safrw.seek(beginLen);
      safrw.read(bs);
      System.out.println("测试直接调用write方法:"+new String(bs));
      if(!"测试直接调用RandomAccessFile的write方法!".equals(new String(bs))){
        super.fail();
      }
      long p = safrw.getFilePointer();
      safrw.insert("Test insert!".getBytes("UTF-8"));
      safrw.insert(0x30);
      safrw.insert("123456789".getBytes("UTF-8"), 1,2);
      safrw.insert("测试!".getBytes("UTF-8"));

      bs = new byte[(int)safrw.length()-(int)p];
      safrw.seek(p);
      int readedcount = safrw.read(bs);
      System.out.println("测试\"rw\"模式,实际读出字节数量:"+readedcount);
      printBytes("测试\"rw\"模式,", bs);
      String str = new String(bs,"UTF-8");
      System.out.println("测试\"rw\"模式,打印文件内容:"+str);
      if(!"Test insert!023测试!".equals(str)){
        super.fail();
      }

    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
     
    }finally{
      closeFile(safrw);
    }
   
    /* 测试"r"模式 */
    try{
      safr = new SmartAccessFile(testedfileR, "r");
      safr.insert("Test insert!".getBytes());
      fail("测试\"r\"模式: 不应该到达此点!");
    }catch(Exception e){
      System.out.println("测试\"r\"模式,应该到达此点!  "+e.getMessage());
    }finally{
      closeFile(safr);
    }

    /* 测试"a"模式 */
    try{
      safa = new SmartAccessFile(testedfileA, "a");
      safa.append("Test insert!".getBytes());
      System.out.println("测试\"a\"模式: 应该到达此点!");
    }catch(Exception e){
      fail("测试\"a\"模式,不应该到达此点!  "+e.getMessage());
    }finally{
      closeFile(safa);
View Full Code Here

Examples of org.fto.jthink.io.SmartAccessFile

  /**
   *方法测试
   */
  public void testWrite() {
    System.out.println("\n[正在测试方法: SmartAccessFile.write()...]");
    SmartAccessFile safrw = null;
    SmartAccessFile safr = null;
    SmartAccessFile safa = null;
   
    /* 测试"rw"模式 */
    try {
      safrw = new SmartAccessFile(testedfileRW, "rw");
     
      byte[] bs = "测试在文件头部写入数据".getBytes();
      safrw.write(bs);
      safrw.seek(0);
      Arrays.fill(bs, (byte)0);
      safrw.read(bs);
      System.out.println("测试在文件头部写入数据:"+new String(bs));
      if(!"测试在文件头部写入数据".equals(new String(bs))){
        super.fail();
      }
      long p = safrw.getFilePointer();
      safrw.write("Test insert!".getBytes("UTF-8"));
      safrw.write(0x30);
      safrw.write("123456789".getBytes("UTF-8"), 1,2);
      safrw.write("测试!".getBytes("UTF-8"));
      bs = new byte[(int)safrw.getFilePointer()-(int)p];
      safrw.seek(p);
      int readedcount = safrw.read(bs);
      System.out.println("测试\"rw\"模式,实际读出字节数量:"+readedcount);
      printBytes("测试\"rw\"模式,", bs);
      String str = new String(bs,"UTF-8");
      System.out.println("测试\"rw\"模式,打印文件内容:"+str);
      if(!"Test insert!023测试!".equals(str)){
        super.fail();
      }

    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
     
    }finally{
      closeFile(safrw);
    }
   
    /* 测试"r"模式 */
    try{
      safr = new SmartAccessFile(testedfileR, "r");
      safr.insert("Test insert!".getBytes());
      fail("测试\"r\"模式: 不应该到达此点!");
    }catch(Exception e){
      System.out.println("测试\"r\"模式,应该到达此点!  "+e.getMessage());
    }finally{
      closeFile(safr);
    }

    /* 测试"a"模式 */
    try{
      safa = new SmartAccessFile(testedfileA, "a");
      safa.append("Test insert!".getBytes());
      System.out.println("测试\"a\"模式: 应该到达此点!");
    }catch(Exception e){
      fail("测试\"a\"模式,不应该到达此点!  "+e.getMessage());
    }finally{
      closeFile(safa);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.