将 CommonsMultipartFile 保存到数据库

发布于 2024-10-20 22:23:09 字数 736 浏览 2 评论 0原文

我正在尝试使用 JPA 将上传的文件保存到数据库。最“自然”的方法(对我来说)是将域对象定义为:

@Entity
class UploadFile {
  ...
  public CommonsMultipartFile getFileData()
  {
    return fileData;
  }
}

但这不起作用,因为没有这样的数据库映射类型。在网上搜索,我发现人们似乎采用了两种方法之一:

  • 将字段定义为 java.sql.blob;
  • 将字段定义为 byte[]

在 < code>@Controller 类,传入的 HttpServletRequest 被强制转换为 MultipartHttpServletRequest,以便访问 MultipartFile 并将其转换回 代码>字节[]流。

然而,使用这个方案,我得到了让我困惑的“随机”结果:时不时地,我遇到“bean属性不可读”错误,byte[]字段上的getter方法的返回类型可能不匹配。我仔细检查了 Bean 的定义,没有发现任何错误。

我想我的问题有两个:(1)知道为什么在这种情况下会发生这个错误吗? (2)更重要的是,处理这样上传的文件的“推荐”方式是什么?

谢谢

奥利弗

I am trying to persist a uploaded file to database using JPA. The most "natural" way (to me) is to define domain object as:

@Entity
class UploadFile {
  ...
  public CommonsMultipartFile getFileData()
  {
    return fileData;
  }
}

But this won't work since there is not such a database mapping type. Searching online, I found that people seems to adapt one of the two methods:

  • Define the field as java.sql.blob;
  • Define the field as byte[]

In the @Controller class, the incoming HttpServletRequest gets cast to MultipartHttpServletRequest in order to access the MultipartFile and convert it back to byte[] stream.

However, with this scheme, I have "random" results which puzzles me: from time to time, I ran into "bean property not readable" error, possible mismatch of return type of getter method on the byte[] field. I double and triple checked my Bean definition, and couldn't find anything wrong.

I guess my questions are two folds: (1) Any idea why this error can happen in this context? (2) more importantly, what are the "recommended" way of handling uploaded file like this?

thanks

Oliver

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

得不到的就毁灭 2024-10-27 22:23:09

您是正确的,java.sql.blobbyte[] 是将上传文件存储在数据库中的最合适方法。

您不应将 MultipartFileCommonsMultipartFile 存储在数据库中,因为它们是临时对象。请注意 MultipartFile javadoc 中的这一点:

文件内容要么存储在内存中,要么临时存储在磁盘上。
无论哪种情况,用户都有责任将文件内容复制到
如果需要,会话级或持久存储。临时存储
将在请求处理结束时清除。

我不太明白您对 HttpServletRequest 所做的事情,但这听起来不是处理上传的最简单方法。处理上传的最简单方法是使 MultipartFile 成为控制器方法上的参数(我认为,如果您使用的是 Spring 3.0 或更高版本):

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void exampleFileUpload(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {
            byte[] fileBytes = file.getBytes();

            // Persist those bytes using JPA here

        } catch (IOException e) {
            // Exception handling here
        }
    }
}

这应该可靠地工作,因为您已经提取了在请求完成之前,也就是在删除用于处理上传的任何临时文件之前,将字节写入您的 byte[] 数组中。

You are correct that either a java.sql.blob or a byte[] would be the most appropriate way to store an uploaded file in a database.

You should not store a MultipartFile or a CommonsMultipartFile in a database because these are intended to be temporary objects. Note this from the MultipartFile javadoc:

The file contents are either stored in memory or temporarily on disk.
In either case, the user is responsible for copying file contents to a
session-level or persistent store as and if desired. The temporary storages
will be cleared at the end of request processing.

I don't quite follow what you're doing with your HttpServletRequest but it doesn't sound like the easiest way to handle the upload. The easiest way to handle an upload is to make the MultipartFile a parameter on your controller method (if you're using Spring 3.0 or later, I think):

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void exampleFileUpload(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {
            byte[] fileBytes = file.getBytes();

            // Persist those bytes using JPA here

        } catch (IOException e) {
            // Exception handling here
        }
    }
}

This should work reliably because you've extracted the bytes into your byte[] array before the request has finished — and thus before the any temporary files used to handle the upload have been deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文