简体   繁体   English

从客户端将文件作为参数发送到REST服务?

[英]Send File as a parameter to a REST Service, from a client?

My Requirement is to send the file to the REST Service through one client. 我的要求是通过一个客户端将文件发送到REST服务。 That service is going to process the file. 该服务将处理文件。 I am using Jersey API for implementing this. 我正在使用Jersey API来实现此目的。 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? 但是我搜索了很多文章,没有关于如何从客户端传递文件以及REST服务将如何检索文件的任何信息...如何实现此目的?

And I am not using the Servlets for Creating REST Service. 而且我没有使用Servlet创建REST服务。

Assuming you are using Jersey on both the client and server side, here is some code that you can extend: 假设您在客户端和服务器端都使用Jersey,则可以扩展以下代码:

Server side: 服务器端:

@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();
    }
}

The above code implements a resource method that accepts POST's of multipart (file) data. 上面的代码实现了一个接受多部分(文件)数据的POST的资源方法。 It also illustrates how you can iterate through all the individual body parts in the incoming (multipart) request. 它还说明了如何遍历传入(多部分)请求中的所有单个身体部位。

Client: 客户:

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);

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. 对于客户端和服务器端代码,都依赖Jersey和JavaMail库。 If you are using Maven, these can be pulled down with ease, with the following dependencies: 如果您使用的是Maven,则可以轻松将其下拉,并具有以下依赖性:

<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>

Adjust the dependency versions as required 根据需要调整依赖项版本

Am I right by assuming, since its a MimeMultipart type, that I could not just send one, but multiple files or additional information maybe as String or whatever, on doing only one simple post, just by adding multiple MimeBodyParts containing the different files or whatever? 我是否假设,因为它是MimeMultipart类型,所以我只能发送一个,而是通过添加多个包含不同文件或其他内容的MimeBodyParts,而只发送一个简单的帖子,而是发送多个文件或其他信息(例如String或其他形式)? ? for example like: 例如:

final MimeMultipart request = new MimeMultipart();
request.addBodyPart(new MimeBodyPart(new FileInputStream(new File(
    fileOne))), 0);
request.addBodyPart(new MimeBodyPart(new FileInputStream(new File(
    fileTwo))), 1);

etc. 等等

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

相关问题 使用rest服务将文件从服务器端发送到客户端 - send a file from the server side to the client side using rest service 使用Spring和Rest Web服务将文件从服务器发送到Client Java - Send file from Server to Client java with Spring and Rest web service 从REST Web服务发送.apk文件到客户端? - send a .apk file from REST web service to client? 将文件从 REST Web 服务发送到客户端的正确方法是什么? - what's the correct way to send a file from REST web service to client? Camel-CXF REST:如何在REST客户端的有效负载上向CXF Web服务发送多个参数? - Camel-CXF REST: How to send to CXF web service more than one parameter on payload in REST client? 如何将参数从prototypejs客户端传递到REST Web服务 - How to pass parameter from prototypejs client to rest web service 使用 JAX-RS 客户端从 REST 服务下载文件 - Download file from REST service using JAX-RS client 如何使用邮递员休息客户端发送对象以调用REST服务 - how to send object using postman rest client to call REST service 如何将pojo对象作为参数传递给prototypejs客户端的其余Web服务 - How to pass pojo object as parameter to rest web service from prototypejs client Spring REST - 创建 ZIP 文件并将其发送到客户端 - Spring REST - create ZIP file and send it to the client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM