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

前端 未结 2 1368
[愿得一人]
[愿得一人] 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

    <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);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-14 23:00

    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.

    0 讨论(0)
提交回复
热议问题