Grails Multipart Service Stream ended unexpectedly
Asked Answered
B

1

1

I'm coding an ios app and I need to send photo from my app to my grails server. In ios i do this:

+(void)sendPhoto:(NSData *)data withName:(NSString *)name onController:(UIViewController<MERequestDelegate> *)controller
{
    MERequest *request = [[MERequest alloc] init];
    request.delegate = controller;

    //User
    User *user = [User loadUser];

    //Creazione del BOdy della richiesta Multipart
    NSMutableData *body = [[NSMutableData alloc] init];

    //Token
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kBOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"token\"\r\n\r\n%@", user.token] dataUsingEncoding:NSUTF8StringEncoding]];

    //Immagine
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kBOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:data]];

    //Chiusura Body
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kBOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];

    [request requestMultipartAtUrl:[NSString stringWithFormat:@"%@WS/sendMultipart", [SettingsUtils webAppUrl]] withData:body];
}

-(void)requestMultipartAtUrl:(NSString *)url withData:(NSData *)body
{
    //NSError *error;
    NSString *stringToBeSent;
    _timeOutSecond = 10;

    stringToBeSent = [[NSString alloc] initWithFormat:@"%@", url];
    NSLog(@"URL: %@", stringToBeSent);

    //Url
    self.theUrl = [NSURL URLWithString:[stringToBeSent stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //Request
    self.mutableRequest = [NSMutableURLRequest requestWithURL:self.theUrl];

    //Header
    [self.mutableRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [self.mutableRequest setHTTPShouldHandleCookies:NO];
    [self.mutableRequest setTimeoutInterval:30];
    [self.mutableRequest setHTTPMethod:@"POST"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", kBOUNDARY];
    [self.mutableRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
    [self.mutableRequest addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];

    //Data
    [self.mutableRequest setHTTPBody:body];

    //Set Timer
    if(_timer == NULL){
        _timer = [NSTimer scheduledTimerWithTimeInterval:_timeOutSecond
                                              target:self
                                            selector:@selector(requestTimedOut:)
                                            userInfo:nil
                                             repeats:NO];
    }

    //Create the connection
    _connection = [[NSURLConnection alloc] initWithRequest:self.mutableRequest delegate:self];
}

In Grails i have a blank method with only a println:

def sendMultipart = {
    println("Send")
}

When i launch the app and create the request i have this error on grails:

2014-02-11 23:28:46,925 [http-8080-1] ERROR [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/SimplifyWFA].[default](StandardWrapperValve.java:281)] - Servlet.service() for servlet default threw exception
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:162)
    at org.codehaus.groovy.grails.web.multipart.ContentLengthAwareCommonsMultipartResolver.parseRequest(ContentLengthAwareCommonsMultipartResolver.java:48)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:138)
    at org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingInfo.getResolvedRequest(DefaultUrlMappingInfo.java:173)
    at org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingInfo.tryMultipartParams(DefaultUrlMappingInfo.java:163)
    at org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingInfo.checkDispatchAction(DefaultUrlMappingInfo.java:140)
    at org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingInfo.getActionName(DefaultUrlMappingInfo.java:122)
    at org.codehaus.groovy.grails.web.mapping.AbstractUrlMappingInfo.populateParamsForMapping(AbstractUrlMappingInfo.java:94)
    at org.codehaus.groovy.grails.web.mapping.AbstractUrlMappingInfo.configure(AbstractUrlMappingInfo.java:44)
    at org.codehaus.groovy.grails.plugins.springsecurity.AnnotationFilterInvocationDefinition.configureMapping(AnnotationFilterInvocationDefinition.java:144)
    at org.codehaus.groovy.grails.plugins.springsecurity.AnnotationFilterInvocationDefinition.determineUrl(AnnotationFilterInvocationDefinition.java:82)
    at org.codehaus.groovy.grails.plugins.springsecurity.AbstractFilterInvocationDefinition.getAttributes(AbstractFilterInvocationDefinition.java:80)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:172)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:106)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:112)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    at org.codehaus.groovy.grails.plugins.springsecurity.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:40)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.codehaus.groovy.grails.plugins.springsecurity.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:79)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:65)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:695)
Caused by: org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
    at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:381)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:155)
    ... 59 more
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
    at org.apache.commons.fileupload.MultipartStream.readHeaders(MultipartStream.java:539)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:976)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.hasNext(FileUploadBase.java:1040)
    at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:356)
    ... 61 more

Why this error appear? It's ios fault? or I missed grails settings?

Thanks!

Bernitabernj answered 11/2, 2014 at 22:31 Comment(0)
B
3

I solved the issue!! THe problem was here:

//Chiusura Body
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kBOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];

Multipart closed body need the "--" after the boundary:

@"\r\n--%@--"

Here we go :D

Thanks to this: Multipart Description

Bernitabernj answered 12/2, 2014 at 15:27 Comment(1)
Thanks, saved the day with the end boundary solution.Marilumarilyn

© 2022 - 2024 — McMap. All rights reserved.