upload.parseRequest(new ServletRequestContext(request))在servlet中返回0个文件

时间:2019-04-10 13:16:04

标签: java jsp servlets

尽管我上传文件,但

My List<FileItems> fileItems = upload.parseRequest(new ServletRequestContext(request))每次仍返回 0 的文件大小。

Servlet代码:-

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的任何建议,我们将不胜感激。我正在尝试仅为每个字段上传单个图像。谢谢。

0 个答案:

没有答案