简体   繁体   English

如何在java的http头中将内容长度设置为long值?

[英]How to set content length as long value in http header in java?

I am writing a web server in java that is transferring file upto 2GB fine. 我正在用java编写一个web服务器,它正在传输高达2GB的文件。 When I searched for the reason, I found like java HttpServelet only allows us to set the content length as int. 当我搜索原因时,我发现java HttpServelet只允许我们将内容长度设置为int。 As the maximum size of integer is 2GB, its working fine upto 2gb when I am using response.setContentLength method. 由于整数的最大大小为2GB,因此当我使用response.setContentLength方法时,它的工作正常可达2gb。 Now the problem is bydefault response.setContentLength has the parameter of integer. 现在的问题是默认的response.setContentLength有整数参数。 So it is not taking long value as parameter. 所以它没有把长值作为参数。 I have already tried response.setHeader("Content-Length", Long.toString(f.length())); 我已经尝试过response.setHeader(“Content-Length”,Long.toString(f.length())); response.addHeader("Content-Length", Long.toString(f.length())); response.addHeader(“Content-Length”,Long.toString(f.length())); but nothing is working. 但没有任何工作。 All time it is failing to add content length when it is a long value. 总是在长值时无法添加内容长度。 So please give any working solution for HTTPServletResponse so that I can set the content length as long value. 所以请为HTTPServletResponse提供任何有效的解决方案,以便我可以将内容长度设置为long值。

You can also use below sample code. 您还可以使用以下示例代码。

long length = fileObj.length();

if (length <= Integer.MAX_VALUE)
{
  response.setContentLength((int)length);
}
else
{
  response.addHeader("Content-Length", Long.toString(length));
}

Try this: 试试这个:

long length = ...;
response.setHeader("Content-Length", String.valueOf(length))

Hope this helps... 希望这可以帮助...

Don't set it at all. 根本不要设置它。

Just let it use chunked transfer mode, which is the default. 只需让它使用分块传输模式,这是默认设置。 There is no Content-Length header in that circumstance. 在这种情况下没有Content-Length标头。 See @BalusC's comment under this question . 请参阅@ BalusC在此问题下的评论。

Do not set it and use chunked encoding. 不要设置它并使用分块编码。 Also beware of HEAD requests = these requests should return the same content-length as GET , but not sending the actual body. 还要注意HEAD请求=这些请求应返回与GET相同的内容长度,但不发送实际正文。 Default implementation of HEAD in javax.servlet.http.HttpServlet is done by calling GET on the same URL and ignoring all the response body written (only counting characters) - see the following fragment: javax.servlet.http.HttpServletHEAD的默认实现是通过在同一URL上调用GET并忽略所有写入的响应主体(仅计数字符)来完成的 - 请参阅以下片段:

protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    NoBodyResponse response = new NoBodyResponse(resp); // mock response (not writing)
    doGet(req, response); // performs a normal GET request
    response.setContentLength(); // this uses INTEGER counter only
}

The problem is that the content length counter is also integer. 问题是内容长度计数器也是整数。 So I recommend overloading doHead method also and not setting the content length at all (you might want to leave GET call also to save time generating the giant file). 所以我建议重载doHead方法,而不是设置内容长度(你可能还想留下GET调用以节省生成巨型文件的时间)。

就像java中的数组一样,在你的情况下你不能超过2 GB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM