コード例 #1
0
  /**
   * creates a new file in a webdav server using the current domain
   *
   * @param path The path relative to the domain for the new file
   * @param inputStream The contents of the new file in local storage or null if the new file is
   *     empty
   * @param overwrite If there is an existing file, it will be overwritten if this parameter is set
   *     to true
   * @return The complete path of the file created or null if not created
   */
  public String createFile(String newPath, InputStream inputStream, boolean overwrite)
      throws IOException {
    String result = null;

    try {
      int len = 0;
      int readLength = 0;
      byte[] inBytes = new byte[DEFAULT_BUFFER_SIZE];
      BufferedInputStream in = new BufferedInputStream(inputStream);

      if (_isLocal) {
        Util.createHigherLevelFolders(_rootFile.getCanonicalPath(), newPath);
        File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + newPath);
        if (!overwrite && tmpFile.exists())
          throw new IOException("File already exist: " + tmpFile.getAbsolutePath());
        FileOutputStream out = new FileOutputStream(tmpFile);

        len = in.read(inBytes);
        while (len != -1) // Check for EOF
        {
          out.write(inBytes, 0, len);
          len = in.read(inBytes);
        }
        out.close();
        result = tmpFile.getCanonicalPath();
      } else {
        len = in.read(inBytes);
        while (len != -1) // Check for EOF
        {
          readLength += len;

          if (readLength == len) { // Write first block of the file
            inBytes = shortenByteArray(inBytes, len);
            result = _remote.createFile(_domain, newPath, inBytes, overwrite);
            if (result == null) throw new IOException("Error creating the file: " + newPath);
          } else { // Continue writing remaining blocks of the file
            boolean last = (len < DEFAULT_BUFFER_SIZE);
            if (last) inBytes = shortenByteArray(inBytes, len);

            int writeLength = (int) writeToFile(newPath, inBytes, last);
            if (writeLength != readLength) {
              throw new Exception("Write length does not match Read Length");
            }
          }
          len = in.read(inBytes);
        }
      }
    } catch (IOException ioe) {
      throw ioe;
    } catch (Exception ex) {
      System.out.println("[FileAccess.createFile] Error uploading file");
      ex.printStackTrace();
    }

    return result;
  }
コード例 #2
0
  /**
   * creates a new file in a webdav server using the current domain
   *
   * @param path The path relative to the domain for the new file
   * @param file The contents of the new file in local storage or null if the new file is empty
   * @param overwrite If there is an existing file, it will be overwritten if this parameter is set
   *     to true
   * @return The complete path of the file created or null if not created
   */
  public String createFile(String newPath, File file, boolean overwrite) throws IOException {
    String result = null;
    try {
      byte[] inBytes = null;
      if (file.length() < DEFAULT_BUFFER_SIZE) inBytes = new byte[(int) file.length()];
      else inBytes = new byte[DEFAULT_BUFFER_SIZE];
      int len = 0;
      BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

      if (_isLocal) {
        Util.createHigherLevelFolders(_rootFile.getCanonicalPath(), newPath);
        File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + newPath);
        if (!overwrite && tmpFile.exists())
          throw new IOException("File already exist: " + tmpFile.getAbsolutePath());
        FileOutputStream out = new FileOutputStream(tmpFile);
        long readLength = 0;
        while (readLength < file.length()) {
          len = in.read(inBytes);
          out.write(inBytes, 0, len);
          readLength += len;
        }
        out.close();
        result = tmpFile.getCanonicalPath();
      } else {
        long readLength = in.read(inBytes);
        result = _remote.createFile(_domain, newPath, inBytes, overwrite);
        if (result == null) throw new IOException("Error creating the file: " + newPath);
        while (readLength < file.length()) {
          len = in.read(inBytes);
          readLength += len;
          boolean last = (readLength >= file.length());
          if (last) { // 040702 DDJ: Modified to shorten byte array
            inBytes = shortenByteArray(inBytes, len);
          }
          if (writeToFile(newPath, inBytes, last) != readLength) {
            throw new Exception();
          }
        }
      }
      in.close();
    } catch (IOException ioe) {
      throw ioe;
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }