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

前端 未结 2 1373
[愿得一人]
[愿得一人] 2020-12-14 22:30

This is what I have so far:

This initializes my REST service

package com.dothatapp.web.rest;

import javax.servlet.annotation.Multip         


        
2条回答
  •  借酒劲吻你
    2020-12-14 22:52

    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

    
        com.web.rest.JaxRsActivator
        
            c:\dotmp
            35000000
            218018841
            0
        
    
    
        com.dothatapp.web.rest.JaxRsActivator
        /rest/*
    
    

    JaxRsActivator.java

        import java.util.HashSet;
        import java.util.Set;
    
        import javax.ws.rs.core.Application;
    
        public class JaxRsActivator extends Application {
              @Override
                public Set> getClasses() {
                    Set> s = new HashSet>();
                    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);
        }
    
    }
    

提交回复
热议问题