这是机架中的错误吗? [英] Is this a bug in Rack?

查看:49
本文介绍了这是机架中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 客户端将多部分内容(一个文件和一些字符串)发布到本地主机上的 Sinatra 服务器.服务器似乎不喜欢 POST 消息.堆栈跟踪是:

I'm trying to post multipart content (a file and some strings) to a Sinatra server on localhost using a java client. It seems the server doesn't like the POST message. The stack trace is:

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'

我的服务器打印出帖子消息中的参数.这是我的 Java 客户端的内容:

My server prints out the params in the post message. Here's what they are for my java client:

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代码是:

The java code I'm using is:

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);

所以当内容类型是字符串时,Rack 似乎不喜欢处理内容类型.当我将 rack/utils.rb 中的 content_type 变量设置为 nil 时,当参数不是文件时,一切正常.这是故意的还是应该作为错误提交?
另请参阅http://blogs.oracle.com/mandy/entry/rack_and_content_type_for.

So it seems Rack doesn't like dealing with content type when the content type is a string. When I set the content_type variable inside rack/utils.rb to nil when the param is not a file everything works fine. Is this intentional or should it be submitted as a bug?
See also http://blogs.oracle.com/mandy/entry/rack_and_content_type_for.

推荐答案

这是一个 Rack 错误,但您可以在 Java 中通过像这样上传来避免它:

This is a Rack bug, but you can avoid it in java by doing your upload like this:

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.

The catch is creating the MultipartEntity with HttpMultipartMode.BROWSER_COMPATIBLE.

这篇关于这是机架中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆