java 通过多部分表单数据上传时如何知道文件大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5920689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know the file size when uploading via multipart form-data?
提问by BreakPhreak
I am exercising a simple application with HTML client that uploads a file to the server and must display a progress bar :)
我正在使用 HTML 客户端执行一个简单的应用程序,该应用程序将文件上传到服务器并且必须显示一个进度条 :)
The server is servlet-based, some servlet receives the multipart content, parses it etc. Obviously, at every moment I know the number of bytes received so far. However in order to supply an info for the progress bar on the client side a servlet also needs the total number of bytes for the file, which is being uploaded. Any idea where to get the total number of bytes from please?
服务器是基于 servlet 的,一些 servlet 接收多部分内容,对其进行解析等。显然,我每时每刻都知道到目前为止收到的字节数。但是,为了在客户端提供进度条的信息,servlet 还需要正在上传的文件的总字节数。知道从哪里获取总字节数吗?
Thanks!
谢谢!
采纳答案by ilalex
Size of content are placed in Content-Length header (see rfc2616). In Servlet you can get this header's value by request.getContentLength()
or reqest.getHeaders("Content-Length")
内容的大小放置在 Content-Length 标头中(参见rfc2616)。在 Servlet 中,您可以通过request.getContentLength()
或获取此标头的值reqest.getHeaders("Content-Length")
回答by Stephen C
I think you have to implement the progress bar in javascript on the client side.
我认为您必须在客户端的 javascript 中实现进度条。
Anyway, this linkgives an explanation of how to do it using JQuery.
无论如何,此链接解释了如何使用 JQuery 进行操作。
The server can obtain the size of the multipart being uploaded from the HTTP request header; see @ilya's answer. However, getting that information and the count(s) of bytes read so far into a stream of HTTP responses that display or update a progress bar would be rather complicated ...
服务器可以从 HTTP 请求头中获取正在上传的 multipart 的大小;请参阅@ilya 的回答。但是,将这些信息和到目前为止读取的字节数获取到显示或更新进度条的 HTTP 响应流中会相当复杂......