No converter for [class xxx] with preset Content-Type ‘multipart/form-data’

Report an error

org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class cn.guet.utils.Result] with preset Content-Type ‘multipart/form-data’

When I put the logic to determine whether the file exists in writeBytes, when it is determined that the file does not exist and BusinessExceptionan exception is thrown, an error is reported in the background.
Because I couldn't handle it at this time response.setContentType("multipart/form-data"), HttpMessageConverterI brought the judgment logic to the front, and the problem was solved.

// 检验文件是否存在
        File file = new File(storagePath);
        log.info("storagePath: {}",storagePath);
        if(!file.exists()){
    
    
            log.info("wfenwogergh======================");
            throw new BusinessException(400,"文件不存在");
        }

        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + downloadName);
        // 现阶段, 没区别
//                "attachment;fileName=" + URLEncoder.encode(downloadName, "utf-8"));

        writeBytes(storagePath,response.getOutputStream());

Guess you like

Origin blog.csdn.net/qq_53318060/article/details/127116665