If you're using Spring you can use the getSize() method.
Here is the definition.
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html
And here some examples
http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/
The last modified date value is part of the filesystem (Operating System), so you can not transfer this metadata with the file.
If you send the value in other field, this is not safe because any user could send whatever value they want. Another thing to notice is that the last modified date depends on the operatingsystem date. Ss if the user change its computer date the date that you recieve it's not real.
Answer from reos on Stack OverflowIf you're using Spring you can use the getSize() method.
Here is the definition.
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html
And here some examples
http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/
The last modified date value is part of the filesystem (Operating System), so you can not transfer this metadata with the file.
If you send the value in other field, this is not safe because any user could send whatever value they want. Another thing to notice is that the last modified date depends on the operatingsystem date. Ss if the user change its computer date the date that you recieve it's not real.
You can use the method transferTo to convert it to a File object and then fetch required properties, because there is no method in MultipartFile class to retrieve lastModified information.
public File multipartToFile(MultipartFile multipart) throws IllegalStateException, IOException
{
File convFile = new File( multipart.getOriginalFilename());
multipart.transferTo(convFile);
return convFile;
}
You can use @HeaderParam to get the value of a named header.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@HeaderParam("Content-Disposition") String contentDisposition
) throws IOException {
Note that you still need to add the file size to the header in the client side, assuming you're using something like the DOM File API and fetch.
You should count buffer size.
....
int size=0;
while ((read = uploadedInputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
size += read.length;
}
System.out.println("size : "+size);
I think you have to implement the progress bar in javascript on the client side.
Anyway, this link gives an explanation of how to do it using 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 ...
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")
Tomcat 7 and and above has a configuration called maxSwallowSize that specify number of bytes for an upload with a default value of 2MB. If your application is configured to accept bigger file, for example with a MultipartResolver in Spring, Tomcat will reject the request.
Because you can configure file size in your application, my advice is to disable maxShallowSize in {TOMCAT_HOME}/conf/server.xml using value -1 and then work only with size configured in your application
as follows:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxSwallowSize="-1"/>
You can find a detailed description here (with spring framerwork)
1. First configure max file upload in Spring ( override default value 1MB)
form spring mvc create bean :
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver=new CommonsMultipartResolver();
resolver.setMaxUploadSize(15728640);
resolver.setMaxUploadSizePerFile(15728640);
return resolver;
}
for spring boot : add in application.properties
spring.servlet.multipart.max-file-size=15MB
spring.servlet.multipart.max-request-size=15MB
2. Second thing configure your tomcat :
locate your server xml tomcat file $TOMCAT_HOME/conf/server.xml
then chnage your connector as below by example :
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxSwallowSize = "-1"/>