所需的请求部分'照片'不在场

时间:2018-02-24 20:39:21

标签: java spring thymeleaf multipartform-data

我在上传图片文件时遇到了这个问题。它说有些东西丢失了,我不知道。我已经搜索了很多东西,但我仍然无法找到解决方案。我试图将其插入数据库并将文件存储在我的项目目录中。似乎我错过了一些东西。

这是我的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>

我的控制员:

@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;

}

1 个答案:

答案 0 :(得分:0)

在“应用程序属性”中添加spring.http.multipart.enabled = true