How can I define a JAX-RS service that processes multi-part data in JEE?

烂漫一生 提交于 2019-11-28 21:33:51
Ioannis Deligiannis

Finally I managed to get this working by without Jersey coupling. The problem is that @Multipart annotation doesn't work with the Application, so you need to define it inside web.xml and inside the Application class define the provided service. Inside the services you can use annotations normally. Also note, that I am extracting the Parts from the request, but this is very easy.

PS. This actually implements the back-end for bluimp JQuery file upload

web.xml

<servlet>
    <servlet-name>com.web.rest.JaxRsActivator</servlet-name>
    <multipart-config>
        <location>c:\dotmp</location>
        <max-file-size>35000000</max-file-size>
        <max-request-size>218018841</max-request-size>
        <file-size-threshold>0</file-size-threshold>
    </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>com.dothatapp.web.rest.JaxRsActivator</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

JaxRsActivator.java

    import java.util.HashSet;
    import java.util.Set;

    import javax.ws.rs.core.Application;

    public class JaxRsActivator extends Application {
          @Override
            public Set<Class<?>> getClasses() {
                Set<Class<?>> s = new HashSet<Class<?>>();
                s.add(FileUpload.class);
                return s;
            }
    }

FileUpload.java

import java.io.IOException;

import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
public class FileUpload {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("fileupload")
    public Response doUpload(@Context HttpServletRequest request) {
        JsonArrayBuilder array = Json.createArrayBuilder();

        try {
            for (Part part : request.getParts()) {
                String name = null;
                long size = 0;
                try {
                    if (part.getContentType() == null
                            || !part.getContentType().toLowerCase()
                                    .startsWith("image/"))
                        continue;

                    name = part.getSubmittedFileName();
                    size = part.getSize();

                    array.add(addFile(name, size, "anId"));
                    part.delete();
                } catch (Exception e) {
                    array.add(addError(name, size, "ERROR"));
                }
            }
        } catch (IOException | ServletException e) {
            e.printStackTrace();
        }

        JsonObject ret = Json.createObjectBuilder().add("files", array).build();
        return Response.status(201).entity(ret).build();
    }

    private JsonObjectBuilder addFile(String name, long size, String url) {
        return Json.createObjectBuilder().add("name", name).add("size", size)
                .add("lid", url);
    }

    private JsonObjectBuilder addError(String name, long size, String error) {
        return Json.createObjectBuilder().add("name", name).add("size", size)
                .add("error", error);
    }

}
Nikos Paraskevopoulos

I believe there is no standard way to handle file uploads with JAX-RS. See this question. File uploading can be done in implementation specific way, check this for Jersey.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!