-1

私は jetty サーバーを使用しており、受け取った 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())、サードパーティ ライブラリ (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 に答える