本文整理汇总了Java中com.google.api.client.util.StreamingContent类的典型用法代码示例。如果您正苦于以下问题:Java StreamingContent类的具体用法?Java StreamingContent怎么用?Java StreamingContent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StreamingContent类属于com.google.api.client.util包,在下文中一共展示了StreamingContent类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeTo
import com.google.api.client.util.StreamingContent; //导入依赖的package包/类
@Override
public void writeTo(OutputStream out) throws IOException {
final OutputStreamWriter writer = new OutputStreamWriter( out, getCharset() );
final String boundary = getBoundary();
final Collection<Part> parts = getParts();
// iterate over each http content part
for ( Part part : parts ) {
final HttpHeaders headers = new HttpHeaders().setAcceptEncoding( null );
final HttpHeaders partHeaders = part.headers;
final HttpContent content = part.content;
final String contentType = content != null ? content.getType() : null;
String contentDisposition = null;
if ( contentType != null && contentType.contains( "multipart" ) && partHeaders != null ) {
// copy all headers if this is a nested multipart content
headers.fromHttpHeaders( partHeaders );
} else {
// we don't need all headers, just null out the unneeded ones
headers.setContentEncoding( null ).setUserAgent( null ).setContentType( null )
.setContentLength( null ).set( CONTENT_TRANSFER_ENCODING, null );
// add mandatory "content-disposition" header
if ( partHeaders != null ) {
contentDisposition = partHeaders.getFirstHeaderStringValue( CONTENT_DISPOSITION );
}
headers.set( CONTENT_DISPOSITION, contentDisposition );
}
// analyze the content
StreamingContent streamingContent = null;
if ( content != null ) {
headers.setContentType( contentType );
headers.set( CONTENT_TRANSFER_ENCODING, "binary" );
final HttpEncoding encoding = part.encoding;
if ( encoding == null ) {
streamingContent = content;
} else {
streamingContent = new HttpEncodingStreamingContent( content, encoding );
}
}
// add boundary string - DON'T put more than one CR_LF between lines
writer.write( TWO_DASHES );
writer.write( boundary );
writer.write( NEWLINE );
// write headers
HttpHeaders.serializeHeadersForMultipartRequests( headers, null, null, writer );
// write content if any
if ( streamingContent != null ) {
writer.write( NEWLINE );
writer.flush();
streamingContent.writeTo( out );
writer.write( NEWLINE );
}
}
// final string boundary
writer.write( TWO_DASHES );
writer.write( boundary );
writer.write( TWO_DASHES );
writer.flush(); // flush before returning
}
示例2: writeTo
import com.google.api.client.util.StreamingContent; //导入依赖的package包/类
@Override
public void writeTo(OutputStream out) throws IOException {
Writer writer = new OutputStreamWriter(out, getCharset());
String boundary = getBoundary();
for (Part part : parts) {
HttpHeaders headers = new HttpHeaders().setAcceptEncoding(null);
if (part.headers != null) {
headers.fromHttpHeaders(part.headers);
}
headers.setContentEncoding(null).setUserAgent(null).setContentType(null).setContentLength(null);
// analyze the content
HttpContent content = part.content;
StreamingContent streamingContent = null;
String contentDisposition = String.format("form-data; name=\"%s\"", part.name);
if (part.filename != null) {
headers.setContentType(content.getType());
contentDisposition += String.format("; filename=\"%s\"", part.filename);
}
headers.set("Content-Disposition", contentDisposition);
HttpEncoding encoding = part.encoding;
if (encoding == null) {
streamingContent = content;
} else {
headers.setContentEncoding(encoding.getName());
streamingContent = new HttpEncodingStreamingContent(content, encoding);
}
// write separator
writer.write(TWO_DASHES);
writer.write(boundary);
writer.write(NEWLINE);
// write headers
HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
// write content
if (streamingContent != null) {
writer.write(NEWLINE);
writer.flush();
streamingContent.writeTo(out);
writer.write(NEWLINE);
}
}
// write end separator
writer.write(TWO_DASHES);
writer.write(boundary);
writer.write(TWO_DASHES);
writer.write(NEWLINE);
writer.flush();
}