[JAVA] Receive multipart / form-data with Jerjey (Jax-rs)

Introduction

In [Android] Uploading images from terminal to server, the receiving side was Servlet. I really wanted to receive it at Jerjey, but I didn't know what to do and compromised. I've figured out how to do this this time, so I'll introduce it.

environment

Correspond to multipart / form-data

Please download the following.

Pass the path through these two.

Create Application class

As it is, multipart / form-data cannot be used yet, so in the package of the project you want to support Create [ApplicationConfig.java] and copy and paste the following.

ApplicationConfig.java


package com.Sample.api;

import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;

public class ApplicationConfig extends ResourceConfig {
   public ApplicationConfig() {
     packages("pakage").register(MultiPartFeature.class);
   }
}

Please enter the package name to be used in "package". Then modify the Web.xml.

Web.xml


<servlet>
  <servlet-name>jersey-app</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <!--Here"pakage.ApplicationConfig"Is the path of the class created above.-->
    <param-value>pakage.ApplicationConfig</param-value>
  </init-param>

  <init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
 </servlet>

This completes multipart / form-data support.

Implementation

Create the following class.

Upload.java


import static java.nio.file.StandardCopyOption.*;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

@Path("/Upload")
public class Upload {
	@POST
	@Path("/File")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	public String upload(@FormDataParam(value = "file") InputStream fileStream,
	                    @FormDataParam(value = "file") FormDataContentDisposition fileDisposition) {
		 try {
		      Files.copy(fileStream, Paths.get("Save destination folder",fileDisposition.getFileName()), REPLACE_EXISTING);
		      return "END";
		    } catch (IOException e) {
		      throw new WebApplicationException(e, 500);
		    }

	}
}

Now http: // localhost / XX / api / Upload / File Send the file to (XX is the package name) and it should be saved.

reference

File upload with JAX-RS! http://blog.yumix.net/entry/2012/12/17/002515

Jersey settings 1 (web.xml, Application class, etc.) https://edgegram.hatenablog.jp/entry/2015/11/25/160433

java --JAX-RS HTTP multipart request https://tutorialmore.com/questions-1757453.htm

Recommended Posts

Receive multipart / form-data with Jerjey (Jax-rs)
POST image with multipart / form-data using Moya