开发者

how to get the file size in servlet

开发者 https://www.devze.com 2023-03-04 02:38 出处:网络
i created a form in jsp which containing some properties about the file like file category, file language,etc after submitting the开发者_如何学C button form variable redirect to servlet,in this servle

i created a form in jsp which containing some properties about the file like file category, file language,etc after submitting the开发者_如何学C button form variable redirect to servlet,in this servlet i want to know the file size.


You can use Common's FileUpload and fileItem.getSize()


According to HTML Form Spec, you need to set the request method to POST and the request encoding to multipart/form-data in the element. If you are using Servlet API 2.5 or earlier, you will have to parse the request yourselves.Apache File Upload API does a good job at that and you can easily get the size of the file. However in case you are using Servlet 3.0 API, the you can use the Servlet API for parsing. You can find the code snippet here


In the servlet you can get the uploaded file size as follows

Part filePart = request.getPart("inputFile");
long fileSize = part.getSize();
0

精彩评论

暂无评论...
验证码 换一张
取 消