2011-07-05 4 views
3

私はJavaクライアントを使ってlocalhost上のSinatraサーバーにマルチパートコンテンツ(ファイルといくつかの文字列)を投稿しようとしています。サーバーがPOSTメッセージを気に入らないようです。スタックトレースは次のとおりです。これはRackのバグですか?

ERROR NoMethodError: undefined method `rewind' for "hi":String 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/utils.rb:581:in`block in parse_multipart' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/utils.rb:499:in`loop' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/utils.rb:499:in`parse_multipart' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/request.rb:270:in `parse_multipart' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/request.rb:148:in `POST' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/methodoverride.rb:15:in `call' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1272:in `block in call' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1303:in `synchronize' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1272:in `call' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/content_length.rb:13:in `call' 
     D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/handler/webrick.rb:52:in `service' 
     D:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' 
     D:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' 
     D:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' 

私のサーバーは、ポストメッセージのパラメータを表示します。

Content-Disposition: form-data; name="file"; filename="fff.jpg" 
Content-Type: image/jpeg 
Content-Transfer-Encoding: binary 
#<File:0xedbc10> 
Content-Disposition: form-data; name="jjj" 
Content-Type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: 8bit 
hi 

私が使用しているJavaのコードは次のとおりです:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://localhost:4567/upload"); 
File file = new File("D:/My Documents/My Desktop/fff.jpg");  
MultipartEntity mpEntity = new MultipartEntity(); 
ContentBody cbFile = new FileBody(file, "image/jpeg"); 
mpEntity.addPart("file", cbFile); 
ContentBody stringBody = new StringBody("hi", Charset.forName("UTF8")); 
mpEntity.addPart("jjj", stringBody); 
httppost.setEntity(mpEntity); 
HttpResponse response = httpclient.execute(httppost); 

だから、ラックは、コンテンツタイプがあるときに、コンテンツの種類を扱って好きではないようです。ここ彼らは私のJavaクライアントのためにあるものです文字列。 paramがファイルでないときにcontent_type変数をnilに設定すると、すべてうまく動作します。これは意図的なのですか、それともバグとして提出すべきですか?
http://blogs.oracle.com/mandy/entry/rack_and_content_type_forも参照してください。

答えて

3

これはラックのバグですが、このようなあなたのアップロードを行うことによって、Javaでそれを避けることができます。

HttpPost httpost = new HttpPost(
       "http://192.168.140.17:3000/ze/api/documents.xml"); 
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("folder_id", new StringBody("3", "multipart/form-data", Charset.defaultCharset()));//f2 
entity.addPart("labels", new StringBody("1,3,4", "form-data", Charset.defaultCharset())); 
entity.addPart("uploaded_data", new FileBody(this.file, filename, "application/pdf", null)); 
httpost.setEntity(entity); 

キャッチがHttpMultipartMode.BROWSER_COMPATIBLEでMultipartEntityを作成しています。

+0

ありがとうございます!なぜそれがアドレスではないのか?バグはどこかに登録されていますか? – Johnny

+0

すばらしい。私のAndroidアプリが別のレールプロジェクトで正常に機能したコードを使って、1つのレールサーバにアップロードできなかったのはなぜだろうか。最初のヒント:同じRails設定ではありません:) – Nuthatch

関連する問題