HTTP-POST图像到Ruby on Rails应用程序

Rau*_*nak 6 java http-post paperclip ruby-on-rails-3

我在java中有一个字节数组形式的图像.我正在尝试将该图像上传到我的Ruby on Rails应用程序,该应用程序使用了Paperclip gem.我的Rails模型如下所示:

class App < ActiveRecord::Base
  has_attached_file :image
end
Run Code Online (Sandbox Code Playgroud)

当我执行java代码时,我得到了HHTTP/1.1 302 Found我的rails应用程序的响应. 更新了java HTTP-Post方法. 这是我用于HTTP-Post的新java代码:

public void postFile(byte[] image) throws ClientProtocolException, IOException{
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost httppost = new HttpPost("http://localhost:3000/apps");

    ContentBody cb = new ByteArrayBody(image, "image/jpg", "icon.jpg");
    //ContentBody cb = new InputStreamBody(new ByteArrayInputStream(image), "image/jpg", "icon.jpg");

    MultipartEntity mpentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    mpentity.addPart("image", cb);
    httppost.setEntity(mpentity);

    HttpResponse response = httpclient.execute(httppost);
    System.out.println(response.getStatusLine());
}
Run Code Online (Sandbox Code Playgroud)

这是rails控制台上的新调试:

Started POST "/apps" for 127.0.0.1 at Mon Jan 31 00:26:07 +0000 2011
Processing by AppsController#create as HTML
Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x1041491c8 @content_type=nil, @original_filename="icon.jpg", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"icon.jpg\"\r\n", @tempfile=#<File:/var/folders/NU/NUcfnxwVHmmnVUo9JUdNok+++TI/-Tmp-/RackMultipart20110131-3735-1ylugl7-0>>}
SQL (0.1ms)  BEGIN
SQL (1.0ms)  describe `apps`
AREL (0.4ms)  INSERT INTO `apps` (`appName`, `image_file_size`, `created_at`, `image_updated_at`, `image_file_name`, `image_content_type`, `updated_at`) VALUES (NULL, NULL, '2011-01-31 00:26:07', NULL, NULL, NULL, '2011-01-31 00:26:07')
[paperclip] Saving attachments.
SQL (1.3ms)  COMMIT
Redirected to http://localhost:3000/apps/36
Completed 302 Found in 18ms
Run Code Online (Sandbox Code Playgroud)

直接从浏览器上传图像就像一个魅力.这是controller/apps_controller.rb的实现:

def create
  @app = App.new(params[:app])

  respond_to do |format|
    if @app.save
      format.html { redirect_to(@app, :notice => 'App was successfully created.') }
      format.xml  { render :xml => @app, :status => :created, :location => @app }
      format.json  { render :json => @app, :status => :created, :location => @app }        
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @app.errors, :status => :unprocessable_entity }
      format.json  { render :json => @app.errors, :status => :unprocessable_entity }        
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

Dan*_*nne 2

我认为问题在于,当没有名为 app 的参数,只有 image 时,您尝试使用 params[:app] 创建应用程序。我认为您的创建操作中的以下内容应该有效:

@app = App.new(:image => params[:image])
Run Code Online (Sandbox Code Playgroud)

另一个好处是在您的应用程序模型中添加以下验证,以确保如果没有保存图像,记录不会保存到数据库中。

validates_attachment_presence :image
Run Code Online (Sandbox Code Playgroud)