0

Apache Commons を使用して画像をアップロードしようとしていますが、エンティティとのバインド時に例外が発生します。

フォームオブジェクト

<sf:form action="${contextPath}client/saveClient" modelAttribute="client" method="POST"
                 enctype="multipart/form-data"> 
            <label>Add Logo</label><input type="file"  name='logo' /><br/>
        </sf:form>

コントローラ

 @RequestMapping("/saveClient")
    public ModelAndView addClient(@RequestParam("logo") MultipartFile file, HttpSession request,
            @Valid Client client, BindingResult result, Model model) throws IOException {

        ModelAndView mvc = new ModelAndView();
        if (result.hasErrors()) {
            mvc.addObject("client", client);
            mvc.setViewName(MANAGECLIENT_PREFIX + "addclient");
            return mvc;
        } else {
            clientService.uploadImage(file, request);
            clientRepository.add(client);
            model.addAttribute("client", client);
            mvc.setViewName(MANAGECLIENT_PREFIX + "saveclient");
            return mvc;
        }
    }

サービス

public void uploadImage(MultipartFile file, HttpSession request) throws IOException {
    logger.info("Enter upload client logo");
    Utilities utilities = new Utilities();
    if (!file.isEmpty()) {
        String fileName = (utilities.getUniqueId() + file.getOriginalFilename());
        System.out.println("Image name: {" + fileName + "}");
        String contextPath = request.getServletContext().getRealPath("/");
        String filePath = contextPath + "\\WEB-INF\\clientlogo\\" + fileName;
        String validFileFormat = utilities.validFileFormat(fileName);
        if (!validFileFormat.isEmpty()) {
            BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
            File destination = new File(filePath);
            ImageIO.write(src, validFileFormat, destination);
            client.setLogo(fileName);
        }
    }
}

実在物

@Column(name = "logo")
private String logo;

例外

Field error in object 'client' on field 'logo': rejected value [org.springframework.web.multipart.commons.CommonsMultipa
rtFile@58d51629]; codes [typeMismatch.client.logo,typeMismatch.logo,typeMismatch.java.lang.String,typeMismatch]; argumen
ts [org.springframework.context.support.DefaultMessageSourceResolvable: codes [client.logo,logo]; arguments []; default
message [logo]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.commons.Co
mmonsMultipartFile' to required type 'java.lang.String' for property 'logo'; nested exception is java.lang.IllegalStateE
xception: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type
 [java.lang.String] for property 'logo': no matching editors or conversion strategy found]}]

イメージ名をDBに、イメージをアプリケーションディレクトリに保存しようとしています。この型変換を解決する方法。

4

3 に答える 3

0

例外は言う

タイプ [org.springframework.web.multipart.commons.CommonsMultipartFile] の値をプロパティ 'logo' の必要なタイプ [java.lang.String] に変換できません: 一致するエディターまたは変換戦略が見つかりません]}]

原因 で定義@RequestParam("logo")しましたMultipartFile parameter

于 2013-02-25T16:44:04.827 に答える
0

これは単なるバインディング エラーです。オブジェクト 'client' には、異なるタイプの 'logo' プロパティがあります。その特定のエラーは無視できます。

使用する:

if (result.hasErrors() && (result.getErrorCount()>1 || !result.hasFieldError("logo")) {
 ....
} 

意味: 複数のエラーがある場合 (常に「ロゴ」フィールドにエラーがある場合)、またはエラーが 1 つでも「ロゴ」エラーでない場合にのみ、エラーに対応する必要があります。

于 2013-03-13T09:57:42.597 に答える