如何使用 jax-rs 进行分段/表单文件上传?
(特别是 RESTeasy)
(对于单个文件)如果有一个像这样的方法签名会很好:
public void upload(@FormParam("name") ..., @FormParam("file") file: InputStream)
...
可行吗?还是我在做梦?似乎没那么简单。
(specifically RESTeasy)
It would be nice (for a single file) to have a method signature like:
public void upload(@FormParam("name") ..., @FormParam("file") file: InputStream)
...
doable? or am I dreaming? doesn't seem to be that simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键是利用 RESTEasy 附带的 @MultipartForm 注释。这使您能够定义包含表单所有部分的 POJO 并轻松绑定它。
以下面的 POJO 为例:
现在您需要做的就是在实体中使用这个 POJO,它看起来像这样:
The key is to leverage the @MultipartForm annotations that comes with RESTEasy. This enables you to define a POJO that contains all the parts of the form and bind it easily.
Take for example the following POJO:
Now all you need to do is use this POJO in the entity which would look something like this:
自 JAX-RS 3.1(正式名称为“Jakarta RESTful Web Services”;Jakarta EE 10 的一部分),您可以这样做:
这不仅是一个标准,而且更易于使用。
请注意,我刚刚发现
@FormParam
在 RestEasy 6.2.7 中不起作用(我刚刚提交了Since JAX-RS 3.1 (officially "Jakarta RESTful Web Services"; part of Jakarta EE 10), you can do:
This is not only a standard, it's also easier to use.
Note that I just found that the
@FormParam
doesn't work here in RestEasy 6.2.7 (I just submitted an issue). You'll have to read all parts and find your own: