upload.parseRequest (новый ServletRequestContext (запрос)) возвращает 0 файлов в сервлете - PullRequest
0 голосов
/ 10 апреля 2019

My List<FileItems> fileItems = upload.parseRequest(new ServletRequestContext(request)) возвращает размер файла 0 каждый раз, несмотря на мою загрузку файлов.

Код сервлета: -

try {
            //if (chiefName != null) {
            File file;
            ServletContext context = getServletContext();
            String filePath = context.getRealPath("/report_uploads");

            System.out.println(filePath);

            File path = new File(filePath);
            if (!path.exists()) {
                path.mkdirs();
            }

            String contentType = request.getContentType();
            if (contentType.contains("multipart/form-data;")) {
                System.out.println("Im inside the multipart loop");
                DiskFileItemFactory factory = new DiskFileItemFactory();

                // Location to save data that is larger than maxMemSize.
                factory.setRepository(new File(filePath));

                // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);

                List<FileItem> fileItems = upload.parseRequest(new ServletRequestContext(request));
                System.out.println("FileItemsSize: " + fileItems.size());
                for (int i = 0; i < fileItems.size(); i++) {
                    FileItem currentFileItem = fileItems.get(i);

                    //Is a file.
                    if (!currentFileItem.isFormField()) {
                        System.out.println("Field name is: " + currentFileItem.getFieldName());
                        if (currentFileItem.getFieldName().equals("chiefSign")) {
                            System.out.println("In chief sign");
                            InputStream is = currentFileItem.getInputStream();
                            if (is != null) {
                                System.out.println("Got data");
                            }
                        }
                    }
                }
            } else {
                System.out.println("Not a multipartformdata");
            }

            //}
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

HTML-код

<tbody>
<tr>
<td><input type="text" id="chiefName" name="chiefName" pattern="[A-Za-z]{1,50}" title="Only letters are allowed." onchange="chiefEngineerName(this);" placeholder="" class="form-control"><span id="cError" class="help-block"></span></td>
<td>
<input type="file" name="chiefSign" accept="image/*" onchange="chiefEngineerSign(this), readURL2(this);
                                                                    capture = camera"><br><br>
<div id="chiefSign"> <img id="displayImg2" height="100" width="100"/></div>
</td>
<td>
<input type="file" name="chiefStamp" accept="image/*" onchange="chiefEngineerStamp(this), readURL3(this);
                                                                    capture = camera" ><br><br>
<div id="chiefStamp"> <img id="displayImg3" height="100" width="100"/></div>
</td>
</tr>
</tbody>

Буду признателен за любые добрые советы о том, почему список файлов возвращает 0 каждый раз.Я пытаюсь загрузить одно изображение только для каждого поля.Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...