Multipart File Upload on Google Appengine using jersey-1.7

前端 未结 5 864
长情又很酷
长情又很酷 2020-11-30 09:11

I wrote an application on Google Appengine with Jersey to handle simple file uploading. This works fine when it was on jersey 1.2. In the later versions (current 1.7) @FormD

5条回答
  •  情书的邮戳
    2020-11-30 09:43

    i've found solution to programmatically avoid to use temporary file creation (very useful for GAE implementation)

    My solution consist of creating a new MultiPartReader Provider ... below my code


      @Provider
        @Consumes("multipart/*")
        public class GaeMultiPartReader implements MessageBodyReader {
    
        final Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
    
        private final Providers providers;
    
        private final CloseableService closeableService;
    
        private final MIMEConfig mimeConfig;
    
        private String getFixedHeaderValue(Header h) {
            String result = h.getValue();
    
            if (h.getName().equals("Content-Disposition") && (result.indexOf("filename=") != -1)) {
                try {
                    result = new String(result.getBytes(), "utf8");
                } catch (UnsupportedEncodingException e) {            
                    final String msg = "Can't convert header \"Content-Disposition\" to UTF8 format.";
                    logger.error(msg,e);
                    throw new RuntimeException(msg);
                }
            }
    
            return result;
        }
    
        public GaeMultiPartReader(@Context Providers providers, @Context MultiPartConfig config,
            @Context CloseableService closeableService) {
            this.providers = providers;
    
            if (config == null) {
                final String msg = "The MultiPartConfig instance we expected is not present. "
                    + "Have you registered the MultiPartConfigProvider class?";
                logger.error( msg );
                throw new IllegalArgumentException(msg);
            }
            this.closeableService = closeableService;
    
            mimeConfig = new MIMEConfig();
            //mimeConfig.setMemoryThreshold(config.getBufferThreshold());
            mimeConfig.setMemoryThreshold(-1L); // GAE FIX
        }
    
        @Override
        public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return MultiPart.class.isAssignableFrom(type);
        }
    
        @Override
        public MultiPart readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType,
            MultivaluedMap headers, InputStream stream) throws IOException, WebApplicationException {
            try {
                MIMEMessage mm = new MIMEMessage(stream, mediaType.getParameters().get("boundary"), mimeConfig);
    
                boolean formData = false;
                MultiPart multiPart = null;
    
                if (MediaTypes.typeEquals(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) {
                    multiPart = new FormDataMultiPart();
                    formData = true;
                } else {
                    multiPart = new MultiPart();
                }
    
                multiPart.setProviders(providers);
    
                if (!formData) {
                    multiPart.setMediaType(mediaType);
                }
    
                for (MIMEPart mp : mm.getAttachments()) {
                    BodyPart bodyPart = null;
    
                    if (formData) {
                        bodyPart = new FormDataBodyPart();
                    } else {
                        bodyPart = new BodyPart();
                    }
    
                    bodyPart.setProviders(providers);
    
                    for (Header h : mp.getAllHeaders()) {
                        bodyPart.getHeaders().add(h.getName(), getFixedHeaderValue(h));
                    }
    
                    try {
                        String contentType = bodyPart.getHeaders().getFirst("Content-Type");
    
                        if (contentType != null) {
                            bodyPart.setMediaType(MediaType.valueOf(contentType));
                        }
    
                        bodyPart.getContentDisposition();
                    } catch (IllegalArgumentException ex) {
                        logger.error( "readFrom error", ex );
                        throw new WebApplicationException(ex, 400);
                    }
    
                    bodyPart.setEntity(new BodyPartEntity(mp));
                    multiPart.getBodyParts().add(bodyPart);
                }
    
                if (closeableService != null) {
                    closeableService.add(multiPart);
                }
    
                return multiPart;
            } catch (MIMEParsingException ex) {
                logger.error( "readFrom error", ex );
                throw new WebApplicationException(ex, 400);
            }
        }
    
    }
    

提交回复
热议问题