Java Example of Downloading Files from a Library Item to Your Local System
This example is based on the code in the ItemDownloadHelper.java sample file.
This example uses the steps that are described in the Download Files to a Local System from a Library Item procedure.
Note: For a complete and
up-to-date version of the sample code, see the
vSphere
Automation SDK Java samples at GitHub.
... // Access the DownloadSession service. DownloadSession downloadSessionService = vapiAuthHelper.getStubFactory().createStub(DownloadSession.class, sessionStubConfig); // Create a new download session model. DownloadSessionModel downloadSessionModel = new DownloadSessionModel(); downloadSessionModel.setLibraryItemId(libItem.getId()); String downloadSessionId = downloadSessionService.create(UUID.randomUUID().toString(), downloadSessionModel); // Access the File service and retrieve the files you want to export. File downloadFileService = vapiAuthHelper.getStubFactory().createStub(File.class, sessionStubConfig); List<FileTypes.Info> downloadFileInfos = downloadFileService.list(downloadSessionId); for (FileTypes.Info downloadFileInfo : downloadFileInfos) { // Make sure all files are in the prepared state before you precede with the downloading operation. downloadFileService.prepare(downloadSessionId, downloadFileInfo.getName(), EndpointType.HTTPS); long timeOut = 360; Long endTime = System.currentTimeMillis() + timeOut * 1000; try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } FileTypes.PrepareStatus expectedStatus = com.vmware.content.library.item.downloadsession.File.PrepareStatus.PREPARED; downloadFileInfo = downloadFileService.get(downloadSessionId, downloadFileInfo.getName()); FileTypes.PrepareStatus currentStatus = downloadFileInfo.getStatus(); if (currentStatus == expectedStatus) { // When the files are prepared, you can retrieve the download information for each file. downloadFileInfo = downloadFileService.get(downloadSessionId, downloadFileInfo.getName()); try { URI downloadUri = downloadFileInfo.getDownloadEndpoint().getUri(); String downloadUrl = downloadUri.toURL().toString(); // Execute an HTTP GET request and pass the download endpoints of the files. HttpClient httpClient = new HttpClient(true); InputStream inputStream = httpClient.downloadFile(downloadUrl); String fileNameDownload = downloadFileInfo.getName(); File tmpDir = new java.io.File("tmp"); tmpDir.mkdir(); String fullPath = tmpDir.getAbsolutePath() + System.getProperty("file.separator") + fileNameDownload; // Copy the files to the directory on your machine. Files.copy(inputStream, Paths.get(fullPath),StandardCopyOption.REPLACE_EXISTING); } catch (MalformedURLException e) { System.out.println("Failed to download due to IOException!" + e); throw new RuntimeException("Failed to download due to IOException!", e); } catch (IOException e) { System.out.println("IO exception during download" + e); throw new RuntimeException("Failed to download due to IOException!", e); // Delete the download session after all files are downloaded. } finally { downloadFileService.delete(downloadSessionId); } } else { while (endTime > System.currentTimeMillis()) { downloadFileInfo = downloadFileService.get(downloadSessionId, downloadFileInfo.getName()); currentStatus = downloadFileInfo.getStatus(); if (currentStatus == expectedStatus) { return; } else if (currentStatus == com.vmware.content.library.item.downloadsession.File.PrepareStatus.ERROR) { System.out.println("DownloadSession Info : " + downloadSessionService.get(downloadSessionId)); throw new RuntimeException("Error while waiting for download file status to be PREPARED..."); } } } }