2

I receive a request (HttpServletRequest req) with 2+ files. I am doing this in order to get files from request

HttpServletRequest req;

....

Map parameters = req.getParameterMap();

UploadedFile uploadedFile = (UploadedFile) parameters.get("file");

String[] serverNames = uploadedFile.getServerFileNames();
debug(serverNames.length);

However I always get only 1 file. What do I do wrong? Thanks.

Dmytro Pastovenskyi
  • 5,240
  • 5
  • 37
  • 56
  • [Upload file using Servlet API](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet). What is `UploadedFile`? Is it from JSF? – xiaofeng.li Nov 23 '16 at 14:00
  • The parameter map has type `Map`, it can not hold the uploaded file. If you are using Servlet API 3.0, you can use `getPart(String)` method. If you are using an older version, you need a library to parse the input stream. – xiaofeng.li Nov 23 '16 at 14:03
  • @luke Lee you are right, but the way it works, files are already uploaded on server and I only receive their file names. UploadedFile class actually contains only file name on server. – Dmytro Pastovenskyi Nov 23 '16 at 14:17

2 Answers2

0

If your parameters contains a list of files, you can use :

for (Entry entry : parameters.entrySet()) {
    if (entry.getValue() instanceof UploadedFile) {
        UploadedFile file = entry.getValue();
    }
}
Tim Weber
  • 858
  • 4
  • 14
0

You use multipart form data and should correctly handle all parts or request. Excample from How to upload files to server using JSP/Servlet?

List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
    //handling...
}
Community
  • 1
  • 1
Vitalii Pro
  • 313
  • 2
  • 17