本文整理汇总了Java中org.apache.http.HttpStatus.SC_PARTIAL_CONTENT属性的典型用法代码示例。如果您正苦于以下问题:Java HttpStatus.SC_PARTIAL_CONTENT属性的具体用法?Java HttpStatus.SC_PARTIAL_CONTENT怎么用?Java HttpStatus.SC_PARTIAL_CONTENT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.apache.http.HttpStatus
的用法示例。
在下文中一共展示了HttpStatus.SC_PARTIAL_CONTENT属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: interruptibleGetRange
/**
* Gets a part of the given URL, writes the content into the given channel.
* Fails if the returned HTTP status is not "206 partial content".
*
* @param <IWC> a generic type for any class that implements InterruptibleChannel and WritableByteChannel
* @param url to get
* @param output written with the content of the HTTP response
* @param etag value of the If-Range header
* @param range_start range byte start (inclusive)
* @param range_end range byte end (inclusive)
*
* @return a response (contains the HTTP Headers, the status code, ...)
*
* @throws IOException IO error
* @throws InterruptedException interrupted
* @throws RuntimeException containing the actual exception if it is not an instance of IOException
*/
public <IWC extends InterruptibleChannel & WritableByteChannel>
HttpResponse interruptibleGetRange(String url, final IWC output, String etag, long range_start, long range_end)
throws IOException, InterruptedException
{
HttpGet get = new HttpGet(url);
get.setHeader("If-Range", etag);
get.setHeader("Range", String.format("bytes=%d-%d", range_start, range_end));
// This validator throws an IOException if the response code is not 206 partial content
ResponseValidator val = new ResponseValidator()
{
@Override
public void validate(HttpResponse response) throws HttpException, IOException
{
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_PARTIAL_CONTENT)
{
throw new IOException("Range request does not return partial content");
}
}
};
return interruptibleRequest(get, output, val);
}
示例2: getStreamLength
private static long getStreamLength(HttpURLConnection connection,
Map<String, List<String>> headers) throws IOException {
String cl = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH);
if (cl == null) {
// Try to get the content length by parsing the content range
// because HftpFileSystem does not return the content length
// if the content is partial.
if (connection.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) {
cl = connection.getHeaderField(HttpHeaders.CONTENT_RANGE);
return getLengthFromRange(cl);
} else {
throw new IOException(HttpHeaders.CONTENT_LENGTH + " is missing: "
+ headers);
}
}
return Long.parseLong(cl);
}
示例3: read
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
try {
final SDSApiClient client = session.getClient();
final HttpUriRequest request = new HttpGet(String.format("%s/nodes/files/%s/downloads", client.getBasePath(),
new SDSNodeIdProvider(session).getFileid(file, new DisabledListProgressListener())));
request.addHeader("X-Sds-Auth-Token", StringUtils.EMPTY);
if(status.isAppend()) {
final HttpRange range = HttpRange.withStatus(status);
final String header;
if(-1 == range.getEnd()) {
header = String.format("bytes=%d-", range.getStart());
}
else {
header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
}
if(log.isDebugEnabled()) {
log.debug(String.format("Add range header %s for file %s", header, file));
}
request.addHeader(new BasicHeader(HttpHeaders.RANGE, header));
// Disable compression
request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity"));
}
final HttpResponse response = client.getClient().execute(request);
switch(response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_PARTIAL_CONTENT:
return new HttpMethodReleaseInputStream(response);
default:
throw new HttpResponseExceptionMappingService().map(new HttpResponseException(
response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
}
}
catch(IOException e) {
throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
}
}
示例4: read
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
try {
if(file.getType().contains(Path.Type.placeholder)) {
final DescriptiveUrl link = new DriveUrlProvider().toUrl(file).find(DescriptiveUrl.Type.http);
if(DescriptiveUrl.EMPTY.equals(link)) {
log.warn(String.format("Missing web link for file %s", file));
return new NullInputStream(file.attributes().getSize());
}
// Write web link file
return IOUtils.toInputStream(UrlFileWriterFactory.get().write(link), Charset.defaultCharset());
}
else {
final String base = session.getClient().getRootUrl();
final HttpUriRequest request = new HttpGet(String.format(String.format("%%s/drive/v3/files/%%s?alt=media&supportsTeamDrives=%s", PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")), base,
new DriveFileidProvider(session).getFileid(file, new DisabledListProgressListener())));
request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE);
if(status.isAppend()) {
final HttpRange range = HttpRange.withStatus(status);
final String header;
if(-1 == range.getEnd()) {
header = String.format("bytes=%d-", range.getStart());
}
else {
header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
}
if(log.isDebugEnabled()) {
log.debug(String.format("Add range header %s for file %s", header, file));
}
request.addHeader(new BasicHeader(HttpHeaders.RANGE, header));
// Disable compression
request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity"));
}
final HttpClient client = session.getHttpClient();
final HttpResponse response = client.execute(request);
switch(response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_PARTIAL_CONTENT:
return new HttpMethodReleaseInputStream(response);
default:
throw new DriveExceptionMappingService().map(new HttpResponseException(
response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
}
}
}
catch(IOException e) {
throw new DriveExceptionMappingService().map("Download {0} failed", e, file);
}
}
示例5: preparePartialDownload
/**
* Prepares to do a partial/resume download.
*
* @param archive The archive we're trying to download.
* @param tmpFile The destination file to download (e.g. something.zip)
* @param propsFile A properties file generated by the last partial download (e.g. .zip.inf)
* @return Null in case we should perform a full download, or a set of headers
* to resume a partial download.
*/
private Header[] preparePartialDownload(Archive archive, File tmpFile, File propsFile) {
// We need both the destination file and its properties to do a resume.
if (mFileOp.isFile(tmpFile) && mFileOp.isFile(propsFile)) {
// The caller already checked the case were the destination file has the
// right size _and_ checksum, so we know at this point one of them is wrong
// here.
// We can obviously only resume a file if its size is smaller than expected.
if (mFileOp.length(tmpFile) < archive.getSize()) {
Properties props = mFileOp.loadProperties(propsFile);
List<Header> headers = new ArrayList<Header>(2);
headers.add(new BasicHeader(HttpHeaders.RANGE,
String.format("bytes=%d-", mFileOp.length(tmpFile))));
// Don't use the properties if there's not at least a 200 or 206 code from
// the last download.
int status = 0;
try {
status = Integer.parseInt(props.getProperty(PROP_STATUS_CODE));
} catch (Exception ignore) {}
if (status == HttpStatus.SC_OK || status == HttpStatus.SC_PARTIAL_CONTENT) {
// Do we have an ETag and/or a Last-Modified?
String etag = props.getProperty(HttpHeaders.ETAG);
String lastMod = props.getProperty(HttpHeaders.LAST_MODIFIED);
if (etag != null && etag.length() > 0) {
headers.add(new BasicHeader(HttpHeaders.IF_MATCH, etag));
} else if (lastMod != null && lastMod.length() > 0) {
headers.add(new BasicHeader(HttpHeaders.IF_MATCH, lastMod));
}
return headers.toArray(new Header[headers.size()]);
}
}
}
// Existing file is either of different size or content.
// Remove the existing file and request a full download.
mFileOp.deleteFileOrFolder(tmpFile);
mFileOp.deleteFileOrFolder(propsFile);
return null;
}