本文整理匯總了Java中com.google.api.client.http.InputStreamContent類的典型用法代碼示例。如果您正苦於以下問題:Java InputStreamContent類的具體用法?Java InputStreamContent怎麽用?Java InputStreamContent使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
InputStreamContent類屬於com.google.api.client.http包,在下文中一共展示了InputStreamContent類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: PostOrderWithTokensOnly
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
/**
* Post an order using the token and secret token to authenticate in DEAL.
* To demonstrate successful authentication a POST request is executed.
*
* @param messageToBePosted the JSON string representing the order
* @return the result of the post action
*
* @throws Exception on an exception occurring
*/
public static String PostOrderWithTokensOnly(String messageToBePosted)
throws Exception
{
// utilize accessToken to access protected resource in DEAL
HttpRequestFactory factory = TRANSPORT
.createRequestFactory(getParameters());
GenericUrl url = new GenericUrl(PROTECTED_ORDERS_URL);
InputStream stream = new ByteArrayInputStream(
messageToBePosted.getBytes());
InputStreamContent content = new InputStreamContent(JSON_IDENTIFIER,
stream);
HttpRequest req = factory.buildPostRequest(url, content);
HttpResponse resp = req.execute();
String response = resp.parseAsString();
// log the response
if (resp.getStatusCode() != 200 && LOG.isInfoEnabled())
{
LOG.info("Response Status Code: " + resp.getStatusCode());
LOG.info("Response body:" + response);
}
return response;
}
示例2: recursiveGCSSync
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
private void recursiveGCSSync(File repo, String prefix, Storage gcs) throws IOException {
for (String name : repo.list()) {
if (!name.equals(".git")) {
File file = new File(repo.getPath(), name);
if (file.isDirectory()) {
recursiveGCSSync(file, prefix + file.getName() + "/", gcs);
} else {
InputStream inputStream = new FileInputStream(repo.getPath() + "/" + file.getName());
InputStreamContent mediaContent = new InputStreamContent("text/plain", inputStream);
mediaContent.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
.setName(prefix + file.getName());
gcs.objects().insert(bucket, objectMetadata, mediaContent).execute();
}
}
}
}
示例3: uploadFile
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
/**
* Uploads data to an object in a bucket.
*
* @param name the name of the destination object.
* @param contentType the MIME type of the data.
* @param file the file to upload.
* @param bucketName the name of the bucket to create the object in.
*/
private void uploadFile(String name, String contentType, File file, String bucketName) throws IOException,
GeneralSecurityException {
InputStreamContent contentStream = new InputStreamContent(contentType, new FileInputStream(file));
// Setting the length improves upload performance
contentStream.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
// Set the destination object name
.setName(name)
// Set the access control list to publicly read-only
.setAcl(Arrays.asList(
new ObjectAccessControl().setEntity("allUsers").setRole("READER")));
// Do the insert
Storage client = StorageFactory.getService();
Storage.Objects.Insert insertRequest = client.objects().insert(
bucketName, objectMetadata, contentStream);
insertRequest.execute();
LogUtils.debug(LOG_TAG, "Successfully uploaded file to bucket.\nName: " + name + "\nBucket name: " +
bucketName);
}
示例4: uploadCache
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
@Override
public void uploadCache(JobOutputProcessor outputProcessor, String cachePath) {
try {
outputProcessor.output("Uploading cache file " + cacheFileName + " to google storage\n");
Storage client = createClient();
File uploadFile = new File(cachePath);
InputStreamContent contentStream = new InputStreamContent(
null, new FileInputStream(uploadFile));
contentStream.setLength(uploadFile.length());
StorageObject objectMetadata = new StorageObject()
.setName(cacheFileName);
Storage.Objects.Insert insertRequest = client.objects().insert(
settings.bucketName, objectMetadata, contentStream);
insertRequest.execute();
outputProcessor.output("Cache uploaded\n");
} catch (GeneralSecurityException | IOException e) {
outputProcessor.output("Error upload cache: " + e.getMessage() + "\n");
}
}
示例5: putBytes
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
/** Upload data. */
public static boolean putBytes(final String bucket, final String key, final byte[] bytes) {
final InputStreamContent mediaContent =
new InputStreamContent("application/octet-stream", new ByteArrayInputStream(bytes));
mediaContent.setLength(bytes.length);
try {
final Storage.Objects.Insert insertObject =
client.objects().insert(bucket, null, mediaContent);
insertObject.setName(key);
if (mediaContent.getLength() > 0
&& mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
}
insertObject.execute();
return true;
} catch (IOException e) {
LOGGER.error("Error uploading data", e);
return false;
}
}
示例6: saveImage
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
/**
* Method to insert a image into the bucket of the cloud storage.
*
* @author <a href="mailto:[email protected]"> João Felipe de Medeiros Moreira </a>
* @since 15/10/2015
*
* @param name of the image.
* @param contentStream to be converted.
*
* @return the media link of the image.
*
* @throws IOException in case a IO problem.
* @throws GeneralSecurityException in case a security problem.
*/
public static String saveImage(String name, InputStreamContent contentStream)
throws IOException, GeneralSecurityException {
logger.finest("###### Saving a image");
StorageObject objectMetadata = new StorageObject()
// Set the destination object name
.setName(name)
// Set the access control list to publicly read-only
.setAcl(Arrays.asList(new ObjectAccessControl().setEntity(ALL_USERS).setRole(READER)));
Storage client = getService();
String bucketName = getBucket().getName();
Storage.Objects.Insert insertRequest =
client.objects().insert(bucketName, objectMetadata, contentStream);
return insertRequest.execute().getMediaLink();
}
示例7: uploadFile
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
/**
* Uploads data to an object in a bucket.
*
* @param name the name of the destination object.
* @param contentType the MIME type of the data.
* @param file the file to upload.
* @param bucketName the name of the bucket to create the object in.
*/
public static void uploadFile(
String name, String contentType, File file, String bucketName)
throws IOException, GeneralSecurityException {
InputStreamContent contentStream = new InputStreamContent(
contentType, new FileInputStream(file));
// Setting the length improves upload performance
contentStream.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
// Set the destination object name
.setName(name)
// Set the access control list to publicly read-only
.setAcl(Arrays.asList(
new ObjectAccessControl().setEntity("allUsers").setRole("READER")));
// Do the insert
Storage client = StorageFactory.getService();
Storage.Objects.Insert insertRequest = client.objects().insert(
bucketName, objectMetadata, contentStream);
insertRequest.execute();
}
示例8: createRequest
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
@Override
public Insert createRequest(InputStreamContent inputStream) throws IOException {
// Create object with the given name and metadata.
StorageObject object =
new StorageObject()
.setMetadata(metadata)
.setName(objectName);
Insert insert = gcs.objects().insert(bucketName, object, inputStream);
writeConditions.apply(insert);
if (insert.getMediaHttpUploader() != null) {
insert.getMediaHttpUploader().setDirectUploadEnabled(isDirectUploadEnabled());
insert.getMediaHttpUploader().setProgressListener(
new LoggingMediaHttpUploaderProgressListener(this.objectName, MIN_LOGGING_INTERVAL_MS));
}
insert.setName(objectName);
return insert;
}
示例9: uploadFile
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
public static com.google.api.services.drive.model.File uploadFile(
java.io.File file, Credential credential, String parent)
throws IOException {
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setParents(Arrays.asList(new ParentReference()
.setId(parent)));
fileMetadata.setTitle(file.getName());
InputStreamContent mediaContent = new InputStreamContent(
"image/png", new BufferedInputStream(new FileInputStream(
file)));
mediaContent.setLength(file.length());
Drive.Files.Insert insert = drive.files().insert(fileMetadata,
mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(true);
return insert.execute();
}
示例10: createFile
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
public File createFile(File file, InputStream content) throws IOException {
logger.debug("creating {}", file);
com.google.api.services.drive.model.File gdFile = file.toGdFile();
Drive.Files.Insert request;
if (file.isDirectory()) {
request = drive.files().insert(gdFile);
} else {
request = drive.files().insert(gdFile, new InputStreamContent(file.getMimeType(), content));
request.getMediaHttpUploader().setDirectUploadEnabled(true);
}
return new File(request.execute());
}
示例11: uploadFile
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
public File uploadFile(String name, String mimeType, java.io.File mediaFile, boolean onWifi)
throws IOException, JSONException {
// File Metadata
File fileMetadata = new File();
fileMetadata.setTitle(name);
fileMetadata.setMimeType(mimeType);
// Set the parent folder.
ParentReference uploadDir = new ParentReference();
uploadDir.setId(findUploadDirectory().getId());
fileMetadata.setParents(Arrays.asList(uploadDir));
InputStreamContent mediaContent = new InputStreamContent(mimeType, new BufferedInputStream(
new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
insert.getMediaHttpUploader().setProgressListener(new ProgressListener(mediaFile));
insert.getMediaHttpUploader().setBackOffPolicyEnabled(true);
int chunkSize = onWifi ? MediaHttpUploader.MINIMUM_CHUNK_SIZE * 2
: MediaHttpUploader.MINIMUM_CHUNK_SIZE;
insert.getMediaHttpUploader().setChunkSize(chunkSize);
return insert.execute();
}
示例12: upload
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
@Override
public void upload(final File thumbnail, final String videoid, final Account account) throws FileNotFoundException, ThumbnailIOException {
if (!thumbnail.exists()) {
throw new FileNotFoundException(thumbnail.getName());
}
final YouTube youTube = youTubeProvider.setAccount(account).get();
try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(thumbnail))) {
youTube.thumbnails()
.set(videoid, new InputStreamContent("application/octet-stream", bufferedInputStream))
.execute();
} catch (final IOException e) {
throw new ThumbnailIOException(e);
}
}
示例13: writeBlob
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
/**
* Writes a blob in the bucket.
*
* @param inputStream content of the blob to be written
* @param blobSize expected size of the blob to be written
*/
void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
SocketAccess.doPrivilegedVoidIOException(() -> {
InputStreamContent stream = new InputStreamContent(null, inputStream);
stream.setLength(blobSize);
Storage.Objects.Insert insert = client.objects().insert(bucket, null, stream);
insert.setName(blobName);
insert.execute();
});
}
示例14: exportOnline
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
/**
* Backup database to google docs
*
* @param drive Google docs connection
* @param targetFolder Google docs folder name
* */
public String exportOnline(Drive drive, String targetFolder) throws Exception {
// get folder first
String folderId = GoogleDriveClient.getOrCreateDriveFolder(drive, targetFolder);
if (folderId == null) {
throw new ImportExportException(R.string.gdocs_folder_not_found);
}
// generation backup file
String fileName = generateFilename();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStream out = new BufferedOutputStream(new GZIPOutputStream(outputStream));
generateBackup(out);
// transforming streams
InputStream backup = new ByteArrayInputStream(outputStream.toByteArray());
InputStreamContent mediaContent = new InputStreamContent(BACKUP_MIME_TYPE, new BufferedInputStream(backup));
mediaContent.setLength(outputStream.size());
// File's metadata.
com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle(fileName);
body.setMimeType(BACKUP_MIME_TYPE);
body.setFileSize((long)outputStream.size());
List<ParentReference> parentReference = new ArrayList<ParentReference>();
parentReference.add(new ParentReference().setId(folderId)) ;
body.setParents(parentReference);
com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();
return fileName;
}
示例15: getStagedURI
import com.google.api.client.http.InputStreamContent; //導入依賴的package包/類
private String getStagedURI(Cluster cluster, String uri) {
URI parsed = URI.create(uri);
if (!isNullOrEmpty(parsed.getScheme()) && !"file".equals(parsed.getScheme())) {
return uri;
}
// either a file URI or just a path, stage it to GCS
File local = Paths.get(uri).toFile();
String configBucket = cluster.getConfig().getConfigBucket();
LOG.debug("Staging {} in GCS bucket {}", uri, configBucket);
try {
InputStreamContent content = new InputStreamContent(
"application/octet-stream", new FileInputStream(local));
content.setLength(local.length()); // docs say that setting length improves upload perf
StorageObject object = new StorageObject()
.setName(Paths.get(GCS_STAGING_PREFIX, local.getName()).toString());
object = client.storage().objects()
.insert(configBucket, object, content)
.execute();
return new URI("gs", object.getBucket(), "/" + object.getName(), null).toString();
} catch (URISyntaxException | IOException e) {
throw Throwables.propagate(e);
}
}