Spring MyBatis 转换错误

Spring MyBatis conversion error

我是 Spring 的 MyBatis 新手,我遇到了这个错误。我正在尝试将图像上传到我的数据库 MySQL

Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile]: no matching editors or conversion strategy found

这是我的控制器:

    @RequestMapping(value = "/GalleryResults", method = RequestMethod.GET)
public String insert(Model model,
        @RequestParam(value = "photo", required = false) CommonsMultipartFile photo,
        @RequestParam(value = "caseNo", required = false) String caseNo,
        @RequestParam(value = "date", required = false) String date,
        @RequestParam(value = "offenseIncident", required = false) String offenseIncident,
        @RequestParam(value = "nameAKA", required = false) String nameAKA,
        @RequestParam(value = "height", required = false) String height,
        @RequestParam(value = "built", required = false) String built,
        @RequestParam(value = "otherInfo", required = false) String otherInfo,
        @RequestParam(value = "describedBy", required = false) String describedBy,
        @RequestParam(value = "requestingParty", required = false) String requestingParty,
        @RequestParam(value = "investOnCase", required = false) String investOnCase,
        @RequestParam(value = "interviewer", required = false) String interviewer,
        @RequestParam(value = "age", required = false) String age,
        @RequestParam(value = "weight", required = false) String weight,
        @RequestParam(value = "complexion", required = false) String complexion,
        @RequestParam(value = "rating", required = false) String rating) {
    try {
        GalleryResults input = new GalleryResults();
        input.setCaseNo(caseNo);
        input.setDate(date);
        input.setOffenseIncident(offenseIncident);
        input.setNameAKA(nameAKA);
        input.setHeight(height);
        input.setBuilt(built);
        input.setOtherInfo(otherInfo);
        input.setDescribedBy(describedBy);
        input.setRequestingParty(requestingParty);
        input.setInvestOnCase(investOnCase);
        input.setInterviewer(interviewer);
        input.setAge(age);
        input.setWeight(weight);
        input.setComplexion(complexion);
        input.setRating(rating);
        input.setPhoto(photo);
        input.setPhotoBytes(photo.getBytes());
        input.setPhotoContentType(photo.getContentType());
        input.setPhotoName(photo.getOriginalFilename());
        galleryService.create(input);
    } catch (Exception e) {
    }

    return "galleryResults";
}

我的POJO

        private CommonsMultipartFile photo;
private byte[] photo;
private byte[] photoBytes;
private String photoName;
private String photoContentType;

有没有我做错了什么?还是我遗漏了什么?

上传文件时,发送到网络服务器的 HTTP 请求如

POST /someUrl Content-Type: multipart/mixed --edt7Tfrdusa7r3lNQc79vXuhIIMlatb7PQg7Vp Content-Disposition: form-data; name="meta-data" Content-Type: application/json; charset=UTF-8 Content-Transfer-Encoding: 8bit { "name": "value" } --edt7Tfrdusa7r3lNQc79vXuhIIMlatb7PQg7Vp Content-Disposition: form-data; name="file-data"; filename="file.properties" Content-Type: text/xml Content-Transfer-Encoding: 8bit ... File Data ...

名为"File Data"的部分代表真实的文件数据流。它被@RequestPart("file-data") 转换为"MultipartFile"。处理上传逻辑的方法应该是这样的:

@RequestMapping("/../upload")
public void uploadFile(@RequestPart("file-data") MultipartFile uploadedFile, ..) {
   ......
}

添加转化策略
org.springframework.web.multipart.commons.CommonsMultipartFile

豆中