@Override public InputStream getInputStream() throws IOException, UnsupportedFileOperationException { VsphereConnHandler connHandler = null; try { connHandler = getConnHandler(); ManagedObjectReference fileManager = getFileManager(connHandler); FileTransferInformation fileDlInfo = connHandler .getClient() .getVimPort() .initiateFileTransferFromGuest(fileManager, vm, credentials, getPathInVm()); String fileDlUrl = fileDlInfo.getUrl().replace("*", connHandler.getClient().getServer()); // http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java URL website = new URL(fileDlUrl); return website.openStream(); } catch (InvalidPropertyFaultMsg e) { translateandLogException(e); } catch (RuntimeFaultFaultMsg e) { translateandLogException(e); } catch (FileFaultFaultMsg e) { translateandLogException(e); } catch (GuestOperationsFaultFaultMsg e) { translateandLogException(e); } catch (InvalidStateFaultMsg e) { translateandLogException(e); } catch (TaskInProgressFaultMsg e) { translateandLogException(e); } finally { releaseConnHandler(connHandler); } return null; }
private URLConnection prepareConnection(String fileUploadUrl, long fileSize) throws RemoteException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, FileFaultFaultMsg, GuestOperationsFaultFaultMsg, InvalidStateFaultMsg, TaskInProgressFaultMsg, MalformedURLException, IOException, ProtocolException { // http://stackoverflow.com/questions/3386832/upload-a-file-using-http-put-in-java URL url = new URL(fileUploadUrl); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); if (conn instanceof HttpURLConnection) { ((HttpURLConnection) conn).setRequestMethod("PUT"); } else { throw new IllegalStateException("Unknown connection type"); } conn.setRequestProperty("Content-type", "application/octet-stream"); conn.setRequestProperty("Content-length", "" + fileSize); return conn; }