如何使用 jax-rs 进行分段/表单文件上传?

发布于 2024-08-29 08:12:11 字数 196 浏览 3 评论 0原文

(特别是 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 技术交流群。

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

发布评论

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

评论(2

梦中楼上月下 2024-09-05 08:12:11

关键是利用 RESTEasy 附带的 @MultipartForm 注释。这使您能够定义包含表单所有部分的 POJO 并轻松绑定它。

以下面的 POJO 为例:

public class FileUploadForm {
    private byte[] filedata;

    public FileUploadForm() {}

    public byte[] getFileData() {
        return filedata;
    }

    @FormParam("filedata")
    @PartType("application/octet-stream")
    public void setFileData(final byte[] filedata) {
        this.filedata = filedata;
    }
}

现在您需要做的就是在实体中使用这个 POJO,它看起来像这样:

@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response create(@MultipartForm FileUploadForm form) 
{
    // Do something with your filedata here
}

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:

public class FileUploadForm {
    private byte[] filedata;

    public FileUploadForm() {}

    public byte[] getFileData() {
        return filedata;
    }

    @FormParam("filedata")
    @PartType("application/octet-stream")
    public void setFileData(final byte[] filedata) {
        this.filedata = filedata;
    }
}

Now all you need to do is use this POJO in the entity which would look something like this:

@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response create(@MultipartForm FileUploadForm form) 
{
    // Do something with your filedata here
}
月依秋水 2024-09-05 08:12:11

JAX-RS 3.1(正式名称为“Jakarta RESTful Web Services”;Jakarta EE 10 的一部分),您可以这样做:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postWidget(
        @FormParam("name") String name,
        @FormParam("file") EntityPart file) {
    var fileName = file.getName().orElseThrow();
    var inputStream = file.getContent();
}

这不仅是一个标准,而且更易于使用。

请注意,我刚刚发现 @FormParam 在 RestEasy 6.2.7 中不起作用(我刚刚提交了

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postWidget(
        List<EntityPart> parts) {
    var file = parts.stream().filter(part -> "file".equals(part.getName())).findFirst().orElseThrow();
    var inputStream = file.getContent();
}

Since JAX-RS 3.1 (officially "Jakarta RESTful Web Services"; part of Jakarta EE 10), you can do:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postWidget(
        @FormParam("name") String name,
        @FormParam("file") EntityPart file) {
    var fileName = file.getName().orElseThrow();
    var inputStream = file.getContent();
}

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:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postWidget(
        List<EntityPart> parts) {
    var file = parts.stream().filter(part -> "file".equals(part.getName())).findFirst().orElseThrow();
    var inputStream = file.getContent();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文