繁体   English   中英

使用 HTML 表单将 BLOB 上传到 MySQL

[英]Uploading a BLOB to MySQL using HTML form

我正在运行 spring web 应用程序,在该应用程序中,有时用户必须在 mysql 数据库中输入大约五个字段。 对于其他所有字段,一切正常,HTML 的表单获取数据,然后 spring 更新数据库,尽管对于 blob 字段。 此 blob 字段必须包含图像、图片。 在 model class 中,我使用 Blob 数据类型存储它。 这就是我要求图片的方式(目的是引导);

<!-- Picture -->
<div class="custom-file">
  <input type="file" class="custom-file-input" name="picture">
  <label class="custom-file-label" for="picture">Select picture...</label>
</div>

虽然这是使用参数接收它的方式;

    @RequestParam(value = "picture", required = false) Blob picture

然后我用 JPA 存储库更新数据库。 但我收到 500 内部服务器错误。 这是信息;

无法将类型“java.lang.String”的值转换为所需类型“java.sql.Blob”; 嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“java.sql.Blob”:找不到匹配的编辑器或转换策略

然后我得到了这个例外;

org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException:无法将“java.lang.String”类型的值转换为所需类型“java.sql.Blob”; 嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“java.sql.Blob”:找不到匹配的编辑器或转换策略

我还想指定,如果我删除图片字段并且不将其添加到数据库中,一切正常,并且在 MySQL 工作台中,我得到一个空白图像,但其他一切都在那里。

我该如何解决这个问题? 有没有更好的方法在 spring java 中存储 Blob? +谢谢大家。

我不确定您为什么在接收有效负载时直接使用 Blob。 理想情况下,您应该使用多部分。 所以,你的代码会像

COntroller 接收多部分文件

@RestController
@RequestMapping("/file")
public class FileHandlingController 
{
    @Autowired
    FileHandlingService fileHandlingSgervice;
    
    @PostMapping(value = "/upload")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public FileInformation uploadDoc(@RequestParam("file") MultipartFile file) throws Exception
    {
        return fileHandlingSgervice.uploadDoc(file);
    }
}

文件处理服务 class 具有上传文件的逻辑

@Service
public class FileHandlingService 
{
    @Autowired
    DBFileStorageService dbFileStorageService;
    
    public FileInformation uploadDoc(MultipartFile file) throws Exception 
    {
        try
        {
            FileInformation fileUploadSuccessData = new FileInformation();
            DBFile dbFile = dbFileStorageService.storeFile(file);
            fileUploadSuccessData.setFileUUId(dbFile.getId());
            fileUploadSuccessData.setFileName(dbFile.getFileName());
            return fileUploadSuccessData;
        }
        catch(MaxUploadSizeExceededException e)
        {
            throw new Exception("File size too large. Max allowed size - 15 MB");
        }
        catch(Exception e)
        {
            throw new Exception("Error uploading file.");
        }
    }
}

将文件推送到数据库的数据库存储服务

@Service
public class DBFileStorageService {

    @Autowired
    private DBFileRepository dbFileRepository;

    public DBFile storeFile(MultipartFile file) {
        // Normalize file name
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());

        try {
            // Check if the file's name contains invalid characters
            if(fileName.contains("..")) {
                //throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName);
            }

            DBFile dbFile = new DBFile(fileName, file.getContentType(), file.getBytes());

            return dbFileRepository.save(dbFile);
        } catch (IOException ex) {
            //throw new FileStorageException("Could not store file " + fileName + ". Please try again!", ex);
        }
        return null;
    }
}

存储库

@Repository
public interface DBFileRepository extends JpaRepository<DBFile, String> {

}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM