当前位置: 首页>>代码示例>>Java>>正文


Java HttpServletResponse.setContentLengthLong方法代码示例

本文整理汇总了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);
}
 
开发者ID:kamax-io,项目名称:mytube-dl,代码行数:14,代码来源:DownloadController.java

示例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);
}
 
开发者ID:equella,项目名称:Equella,代码行数:12,代码来源:JSONExporter.java

示例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());
    }
}
 
开发者ID:JesseHu1520,项目名称:mall,代码行数:12,代码来源:AdminProductController.java

示例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);
    }
  }
}
 
开发者ID:adobe,项目名称:S3Mock,代码行数:41,代码来源:FileStoreController.java

示例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);
    }
  }
}
 
开发者ID:adobe,项目名称:S3Mock,代码行数:42,代码来源:FileStoreController.java

示例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);
}
 
开发者ID:Team-Sprout,项目名称:Clipcon-Server,代码行数:74,代码来源:DownloadServlet.java


注:本文中的javax.servlet.http.HttpServletResponse.setContentLengthLong方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。