-1

我正在使用码头服务器,我一直在尝试读取作为表单附加到我收到的 POST 请求中的图像。我尝试了一些模式,但我总是遇到异常。

//Process POST requests to upload images
private void handleImage(String target, Request baseRequest, HttpServletRequest request,
            HttpServletResponse response) throws Exception{

            //Get image
        byte[] pictureInByte = (request.getParameter("image")).getBytes();  
        InputStream in = new ByteArrayInputStream(pictureInByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);

            //Save to disk
        ImageIO.write(bImageFromConvert, "jpeg", new File(
                "new-darksouls.jpg"));



    }

这就是 POST 的创建方式(这是在 iPhone 上使用 Obj-C)。我在 Jetty 服务器上收到请求,但无法读取。

//Set http client defaults
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:AS_NET_BASE_URL]];
        [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
        [httpClient setParameterEncoding:AFJSONParameterEncoding];
        [httpClient defaultValueForHeader:@"Accept"];

        //init http request
        NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:route parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

            //Append data (JSON)
            [formData appendPartWithFileData:image
                                        name:@"image"
                                    fileName:@"2"
                                    mimeType:@"image/jpeg"];
        }];

        //Time out
        [request setTimeoutInterval:AS_MI_NET_TIMEOUT_INTERVAL];

        //Init operation
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

        //Set completion blocks
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            //Stop indicator
            [self stopNetworkIndicator];

            completion(YES, responseObject);

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            [self stopNetworkIndicator];

            NSLog(@"%@", error);

            completion(NO, NULL);
        }];

        //Start spinning
        [self startNetworkIndicator];

        //Send request
        [operation start];

        //No internet, finish method
    }else{
        completion(NO, NULL);
    }
4

1 回答 1

1

该类AFMultipartFormData创建一个多部分请求

我认为 Jetty 还不支持 Servlet 3.0 规范(这会让您使用request.getParts()),但您可以使用 3rd 方库(Spring/Apache commons)来解析多部分请求。

搜索“apache commons fileupload multipart”应该会找到一些示例。

如果您使用的是 Spring,请查看http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch16s08.html

于 2013-10-27T01:42:32.073 回答