简体   繁体   English

所需的要求部分“照片”不存在

[英]Required request part 'photo' is not present

I encountered this issue on uploading image file. 我在上传图片文件时遇到此问题。 It say's that there is something part is missing and I have no idea. 它说是有些东西丢失了,我不知道。 I have searched so many things already but still I couldn't find a solution. 我已经搜索了很多东西,但仍然找不到解决方案。 I'm trying to insert it in database and store the file in my project directory. 我试图将其插入数据库并将文件存储在我的项目目录中。 It's seems I have missed something. 好像我错过了什么。

here is my html: 这是我的HTML:

    <form autocomplete="off" th:action="@{/AddCriminal}"
         enctype="multipart/form-data" method="post" class="m-t" role="form"
        th:object="${criminalRec}" data-toggle="validator">

        <h1 class="text-white">Add Criminal</h1>

        <div th:if="${info}" class="alert alert-success" role="alert"
            th:text=${info}></div>
        <div th:if="${infoError}" class="alert alert-danger" role="alert"
            th:text="${infoError}"></div>

        <div class="row text-center">
            <div class="col-md-5">
                <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}"
                    class="validation-message alert alert-danger" role="alert"></div>

                <div th:if="${#fields.hasErrors('seq_number')}"
                    th:errors="*{seq_number}"
                    class="validation-message alert alert-danger" role="alert"></div>

                <div th:if="${#fields.hasErrors('comments')}"
                    th:errors="*{comments}"
                    class="validation-message alert alert-danger" role="alert"></div>

                <div class="form-group">
                    <label class="text-white">Full Name: </label> <input type="text"
                        th:field="*{name}" placeholder="Wanted Full name"
                        class="form-control" required /> <small id="firstnameHelp"
                        class="form-text text-muted text-white">Full name of the
                        person</small>
                </div>

                <div class="form-group">
                    <label class="text-white">Sequence Number: </label> <input
                        type="text" th:field="*{seq_number}"
                        placeholder="Sequence Number" class="form-control" required /> <small
                        id="firstnameHelp" class="form-text text-muted text-white">Sequence
                        of the records the Ascending order</small>
                </div>

                <div class="form-group">
                    <label class="text-white">Photo: </label> <!-- <input type="file"
                        th:field="*{photo}" placeholder="Add Photo" class="form-control"
                        accept="image/*" required /> -->
                     <input type="file" name="photo" accept="image/*" class="form-control" />
                    <small id="firstnameHelp" class="form-text text-muted text-white">Upload
                        Photo</small>
                </div>
            </div>

            <div class="col-md-12">
                <div class="form-group">
                    <textarea class="form-control" th:field="*{comments}"
                        placeholder="Facts" rows="3" required></textarea>
                    <small id="firstnameHelp" class="form-text text-muted text-white">Facts
                        about this criminal</small>
                </div>
            </div>

            <div class="col-md-5">
                <button type="submit" class="btn btn-primary block full-width m-b">Add
                    Criminal</button>
            </div>

        </div>

    </form>

my controller: 我的控制器:

@RequestMapping(value = "/AddCriminal", method = RequestMethod.POST, consumes = "multipart/form-data")
public ModelAndView processCriminal(ModelAndView modelAndView,
        @Valid @ModelAttribute("criminalRec") Criminals criminalRec, @RequestParam("photo") MultipartFile file,
        BindingResult bindingResult, HttpServletRequest request)
        throws SerialException, SQLException, IOException {

    if (bindingResult.hasErrors()) {
        modelAndView.setViewName("/admin/addwantedperson");
    } else {            
        storageService.store(file);
        System.out.println("FILENAME: " + storageService.getFName());

        byte [] byteArr=file.getBytes();
        Blob blob = new SerialBlob(byteArr);
        criminalRec.setPhoto(blob);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
        LocalDate localDate = LocalDate.now();

        criminalRec.setDate_added(formatter.format(localDate));

        criminalService.saveCriminal(criminalRec);
        modelAndView.addObject("info", "Criminal Record Successfully Added!");
        modelAndView.addObject("criminalRec", new Criminals());
        modelAndView.setViewName("/admin/addwantedperson");
    }

    return modelAndView;

}

在您的应用程序属性中,添加spring.http.multipart.enabled = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot - MissingServletRequestPartException: Required request part 'file' is not present Springboot Spring Thymeleaf所需的请求部分“文件”不存在 - Spring Thymeleaf Required request part 'file' is not present Tomcat:所需的请求部分“文件”不存在 - Tomcat : Required request part 'file' is not present 出现“不存在所需的请求部分&#39;文件&#39;”错误 - Getting “Required request part 'file' is not present” error Spring 文件上传 - “所需的请求部分不存在” - Spring File Upload - 'Required request part is not present' Spring Boot Required 请求部分“文件”不存在 - Spring Boot Required request part 'file' is not present 上传文件springboot所需的请求部分“文件”不存在 - upload file springboot Required request part 'file' is not present 如何修复 Spring 引导分段上传中的“所需请求部分不存在” - How to fix "Required request part is not present" in a Spring Boot multipart upload React 和 Spring 引导不存在所需的请求部分“图像” - Required request part 'image' is not present with React and Spring Boot 此请求部分“文件”不存在 - This request part 'file' is not present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM