JAVA文件上传的实现类

/**
   * 文件上传的实现类,将源文件名去掉后用时间作为名字存放
   * @param len          request的长度
   * @param contentType  request的内容
   * @param in           request流
   * @return byte[]      上传文件的字节数组
   */
  public byte[] uploadToByte(int len,String contentType,InputStream in)
  {
      String upFile = "";
      try
      {
          byte buffer[] = new byte[len];

          int start = contentType.indexOf("boundary=");
          int boundaryLen = new String("boundary=").length();
          String boundary = contentType.substring(start + boundaryLen);
          boundary = "--" + boundary;

          int total = 0;
          int once = 0;
          while ( (total < len) && (once >= 0))
          {
              once = in.read(buffer, total, len);
              total += once;
          }


          String[] tokens =
              {
              "name=/"", "/"; filename=/"", "/"/r/n", "Content-Type: ",
              "/r/n/r/n"};

          //pos0 记录 boundary 的第一个字节在buffer 中的位置
          int pos0 = byteIndexOf(buffer, boundary, 0);
          int pos1 = 0; //pos1 记录在buffer 中下一个 boundary 的位置
          pos0 += boundaryLen; //记录boundary后面第一个字节的下标
          pos1 = byteIndexOf(buffer, boundary, pos0);
          pos0 += 2; //考虑到boundary后面的 /r/n
          buffer = subBytes(buffer, pos0, pos1 - 2);

          int[] position = new int[tokens.length];
          for (int i = 0; i < tokens.length; i++)
          {
              position[i] = byteIndexOf(buffer, tokens[i], 0);
          }

          //得到字段名
          String name = subBytesString(buffer,
                                       position[0] + bytesLen(tokens[0]),
                                       position[1]);

          //得到文件名
          String filename = subBytesString(buffer,
                                           position[1] + bytesLen(tokens[1]),
                                           position[2]);
          System.out.println("filename=" + filename); //eg:filename="c:/set.sql"

          //上传文件的路径
          int size = 0;

          for (int k = 0; k < filename.length(); k++)
          {
              if (filename.charAt(k) == '//')
              {
                  size = k;
              }
          }
          //System.out.println("the start position is:" + size);

          String myFile = filename.substring(size + 1, filename.length());

//            System.out.println("myFile = " + myFile);
//            int a = myFile.indexOf(".");
//            if (a > 0 )
//                myFile = myFile.substring(a);

//            long ltime = 0L;
//            Date dtime = new Date();
//            ltime = dtime.getTime(); //得到这个标识
//            String stime = ltime+"";//将这个标识转换成一个字符对象
//            //System.out.println("the file name is:" + stime + myFile);
//
//            //String writePath = "c://logs//" + stime + "_" + myFile;
//            //String readPath  = "c://logs//" + stime + "_" + myFile;
//            String writePath = Write_Photo_Path + stime+"_"+myFile;
//            String readPath  = Read_Photo_Path + stime + "_" + myFile;
//            //该文件的名称
//            this.file_name = stime + "_" + myFile;
            //String writePath = Write_Photo_Path +myFile;
            //String readPath  = Read_Photo_Path + myFile;
//            //该文件的名称
            this.file_name = myFile;


          //得到 Content-Type
          String content_Type = subBytesString(buffer,
                                               position[3] +
                                               bytesLen(tokens[3]),
                                               position[4]);
          //System.out.println("content_Type=" + content_Type);

          //得到文件内容
          byte[] b = subBytes(buffer, position[4] + bytesLen(tokens[4]),
                              buffer.length);
          //System.out.println(position[4] + bytesLen(tokens[4]));
          //System.out.println(buffer.length);
          this.bContent = b;//得到文件的BYTE数组
      }
      catch (Exception e)
      {
          e.printStackTrace();
          System.out.println("C:UploadBean;M:uploadToByte()-->" + e.getMessage());
      }
      return this.bContent;
  }







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值