从客户端将文件作为参数发送到REST服务? [英] Send File as a parameter to a REST Service, from a client?

查看:111
本文介绍了从客户端将文件作为参数发送到REST服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是通过一个客户端将文件发送到REST服务.该服务将处理文件.我正在使用Jersey API来实现此目的.但是我搜索了很多文章,没有关于如何从客户端传递文件 REST服务如何检索文件的任何信息.实现这一目标?

My Requirement is to send the file to the REST Service through one client. That service is going to process the file. I am using Jersey API for implementing this. But I have searched in many articles, there is no any information of how to pass the file from client side and how the REST service will retrieve the file... How to achieve this?

而且我没有使用Servlet创建REST服务.

And I am not using the Servlets for Creating REST Service.

推荐答案

假定您在客户端和服务器端都使用Jersey,下面是一些可以扩展的代码:

Assuming you are using Jersey on both the client and server side, here is some code that you can extend:

服务器端:

@POST
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(final MimeMultipart file) {
    if (file == null)
        return Response.status(Status.BAD_REQUEST)
                .entity("Must supply a valid file").build();

    try {
        for (int i = 0; i < file.getCount(); i++) {
            System.out.println("Body Part: " + file.getBodyPart(i));
        }
        return Response.ok("Done").build();
    } catch (final Exception e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e)
                .build();
    }
}

以上代码实现了一种资源方法,该方法接受POST(多部分(文件)数据).它还说明了如何遍历传入(多部分)请求中的所有单个身体部位.

The above code implements a resource method that accepts POST's of multipart (file) data. It also illustrates how you can iterate through all the individual body parts in the incoming (multipart) request.

客户:

final ClientConfig config = new DefaultClientConfig();
final Client client = Client.create(config);

final WebResource resource = client.resource(ENDPOINT_URL);

final MimeMultipart request = new MimeMultipart();
request.addBodyPart(new MimeBodyPart(new FileInputStream(new File(
        fileName))));

final String response = resource
    .entity(request, "multipart/form-data")
    .accept("text/plain")
    .post(String.class);

上面的代码只是将文件附加到多部分请求,然后将请求触发到服务器.对于客户端和服务器端代码,都依赖Jersey和JavaMail库.如果您使用的是Maven,则可以轻松将其下拉,并具有以下依赖性:

The above code simply attaches a file to a multipart request, and fires the request off to the server. For both client and server side code there is a reliance on the Jersey and JavaMail libraries. If you are using Maven, these can be pulled down with ease, with the following dependencies:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.17</version>
</dependency>

<dependency> <!-- only on server side -->
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.14</version>
</dependency>

<dependency> <!-- only on client side -->
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.17</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.17</version>
</dependency>

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.6</version>
</dependency>

根据需要调整依赖项版本

这篇关于从客户端将文件作为参数发送到REST服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆