Java 类com.vmware.vim25.GuestPosixFileAttributes 实例源码

项目:jcloud-vsphere    文件:GuestFilesUtils.java   
public static boolean loadFileToGuest(VirtualMachine vm, InputStream in, NamePasswordAuthentication npa, VSphereServiceInstance serviceInstance, String fullPathToFileInGuest) {
   try {
      GuestPosixFileAttributes posixFileAttributes = new GuestPosixFileAttributes();
      posixFileAttributes.setPermissions(Long.valueOf(500));
      posixFileAttributes.setAccessTime(GregorianCalendar.getInstance());
      Calendar modCal = Calendar.getInstance();
      modCal.setTimeInMillis(System.currentTimeMillis());
      posixFileAttributes.setModificationTime(modCal);
      int fileSize = in.available();
      String upUrlStr = serviceInstance.getInstance().getGuestOperationsManager().getFileManager(vm).initiateFileTransferToGuest(npa, fullPathToFileInGuest, posixFileAttributes, fileSize, true);
      //upUrlStr.replace("\\*", serviceInstance.getInstance().getServerConnection().getUrl().getHost());
      HttpURLConnection putCon = (HttpURLConnection) new URL(upUrlStr).openConnection();
      putCon.setDoInput(true);
      putCon.setDoOutput(true);
      putCon.setRequestProperty("Content-Type", "application/octet-stream");
      putCon.setRequestMethod("PUT");
      putCon.setRequestProperty("Content-Length", Long.toString(fileSize));

      byte[] buffer = new byte[VSphereRestClient.CHUNKLEN];
      if (fileSize > VSphereRestClient.CHUNKLEN)
         putCon.setChunkedStreamingMode(VSphereRestClient.CHUNKLEN);
      else {
         putCon.setChunkedStreamingMode(fileSize);
         buffer = new byte[fileSize];
      }

      DataOutputStream out = new DataOutputStream(putCon.getOutputStream());
      int len = 0;
      while ((len = in.read(buffer)) > 0) {
         out.write(buffer, 0, len);
      }
      if (out != null) {
         out.flush();
         out.close();
      }
      if (in != null)
         in.close();
   return true;
   } catch (Exception e) {
      return false;
   }
}