本文整理匯總了Java中javax.servlet.http.HttpServletResponse.setContentLengthLong方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletResponse.setContentLengthLong方法的具體用法?Java HttpServletResponse.setContentLengthLong怎麽用?Java HttpServletResponse.setContentLengthLong使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.servlet.http.HttpServletResponse
的用法示例。
在下文中一共展示了HttpServletResponse.setContentLengthLong方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: download
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@RequestMapping(method = POST, path = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void download(HttpServletResponse res, @RequestParam String url) throws IOException, InterruptedException, TimeoutException {
URL target = new URL(url);
String filename = mtdl.download(target);
File file = new File(filename);
res.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
res.setContentLengthLong(FileUtils.sizeOf(file));
IOUtils.copy(new FileInputStream(filename), res.getOutputStream());
res.flushBuffer();
FileUtils.deleteQuietly(file);
}
示例2: handleRequest
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
@SuppressWarnings("nls")
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException
{
Object result = service.getResult(request);
String json = gson.toJsonTree(result).toString();
response.setContentType("text/json");
response.setContentLengthLong(json.getBytes().length);
response.getWriter().write(json);
}
示例3: getImage
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@RequestMapping(method = RequestMethod.GET, value = "/img/{filename:.+}")
public void getImage(@PathVariable(name = "filename", required = true) String filename,
HttpServletResponse res) throws IOException {
File file = new File("file/" + filename);
if (file != null && file.exists()) {
res.setHeader("content-type", "application/octet-stream");
res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
res.setContentLengthLong(file.length());
Files.copy(Paths.get(file.toURI()), res.getOutputStream());
}
}
示例4: getObject
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Returns the File identified by bucketName and fileName
*
* http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html
*
* @param bucketName The Buckets names
* @param range byte range
* @param response response object
* @throws IOException If an input or output exception occurs
*/
@RequestMapping(
value = "/{bucketName:.+}/**",
method = RequestMethod.GET,
produces = "application/x-www-form-urlencoded")
public void getObject(@PathVariable final String bucketName,
@RequestHeader(value = RANGE, required = false) final Range range,
final HttpServletRequest request,
final HttpServletResponse response) throws IOException {
final String filename = filenameFrom(bucketName, request);
final S3Object s3Object = fileStore.getS3Object(bucketName, filename);
if (s3Object == null) {
LOG.error("Object could not be found!");
response.sendError(404,
String.format("File %s in Bucket %s couldn't be found!", filename, bucketName));
} else if (range != null) {
getObjectWithRange(response, range, s3Object);
} else {
response.setHeader(HttpHeaders.ETAG, "\"" + s3Object.getMd5() + "\"");
response.setContentType(s3Object.getContentType());
response.setContentLengthLong(s3Object.getDataFile().length());
response.setHeader(HttpHeaders.ACCEPT_RANGES, RANGES_BYTES);
response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, ANY);
try (OutputStream outputStream = response.getOutputStream()) {
Files.copy(s3Object.getDataFile().toPath(), outputStream);
}
}
}
示例5: getObjectWithRange
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* supports range different range ends. eg. if content has 100 bytes, the range request could
* be: bytes=10-100,
* 10--1 and 10-200
*
* see: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html
*
* @param response {@link HttpServletResponse}
* @param range {@link String}
* @param s3Object {@link S3Object}
* @throws IOException if invalid range request value
*/
private void getObjectWithRange(final HttpServletResponse response, final Range range,
final S3Object s3Object)
throws IOException {
final long fileSize = s3Object.getDataFile().length();
final long bytesToRead = Math.min(fileSize - 1, range.getEnd()) - range.getStart() + 1;
if (bytesToRead < 0 || fileSize < range.getStart()) {
response.setStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value());
response.flushBuffer();
return;
}
response.setStatus(HttpStatus.PARTIAL_CONTENT.value());
response.setHeader(HttpHeaders.ACCEPT_RANGES, RANGES_BYTES);
response.setHeader(HttpHeaders.CONTENT_RANGE,
String.format("bytes %s-%s", range.getStart(), bytesToRead + range.getStart() - 1));
response.setHeader(HttpHeaders.ETAG, "\"" + s3Object.getMd5() + "\"");
response.setDateHeader(HttpHeaders.LAST_MODIFIED, s3Object.getLastModified());
response.setContentType(s3Object.getContentType());
response.setContentLengthLong(bytesToRead);
try (OutputStream outputStream = response.getOutputStream()) {
try (FileInputStream fis = new FileInputStream(s3Object.getDataFile())) {
fis.skip(range.getStart());
IOUtils.copy(new BoundedInputStream(fis, bytesToRead), outputStream);
}
}
}
示例6: doGet
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("===================================================================\ndoGet START");
// get Request Data
String userName = request.getParameter("userName");
String groupPK = request.getParameter("groupPK");
String downloadDataPK = request.getParameter("downloadDataPK");
System.out.println("<<Parameter>>\n userName: " + userName + ", groupPK: " + groupPK + ", downloadDataPK: " + downloadDataPK + "\n");
Group group = server.getGroupByPrimaryKey(groupPK);
Contents contents = group.getContents(downloadDataPK);
String contentsType = contents.getContentsType();
switch (contentsType) {
case Contents.TYPE_STRING:
String stringData = contents.getContentsValue();
response.setContentType("text/plain; charset=UTF-8");
response.setHeader("Content-Disposition", "form-data; name=stringData" + "\"" + LINE_FEED);
sendStringData(stringData, response.getOutputStream());
break;
case Contents.TYPE_IMAGE:
String imageFileName = contents.getContentsPKName();
setHeaderForSendingFile("image/png", imageFileName, response);
// Get file data to send to client
File sendImageFileContents = new File(ROOT_LOCATION + groupPK + File.separator + imageFileName);
response.setContentLengthLong(sendImageFileContents.length());
// Transfer the image file data in the directory. (ByteArrayStream)
sendFileData(sendImageFileContents, groupPK, response.getOutputStream());
break;
case Contents.TYPE_FILE:
String fileName = contents.getContentsPKName();
setHeaderForSendingFile("application/octet-stream", fileName, response);
// Get file data to send to client
File sendFileContents = new File(ROOT_LOCATION + groupPK + File.separator + fileName);
response.setContentLengthLong(sendFileContents.length());
// Transfer the file data in the directory. (FileStream)
sendFileData(sendFileContents, groupPK, response.getOutputStream());
break;
case Contents.TYPE_MULTIPLE_FILE:
String multipleFileName = contents.getContentsPKName();
setHeaderForSendingFile("application/octet-stream", multipleFileName, response);
// Get file data to send to client
File sendMultipleFileContents = new File(ROOT_LOCATION + groupPK + File.separator + multipleFileName);
response.setContentLengthLong(sendMultipleFileContents.length());
// Transfer the zip(multiple) file data in the directory. (FileStream)
sendFileData(sendMultipleFileContents, groupPK, response.getOutputStream());
break;
default:
System.out.println("<<DOWNLOAD SERVLET>> It does not belong to any format.");
}
// TmpLog.responseMsgLog(response);
}