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

标签 java ruby-on-rails-3 paperclip http-post

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

class App < ActiveRecord::Base
  has_attached_file :image
end

当我执行 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());
}

这是 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

直接从浏览器上传图片非常棒。下面是 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

我哪里错了?

最佳答案

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

@app = App.new(:image => params[:image])

另一件好事是在您的 App 模型中添加以下验证,以确保如果没有保存图像,则不会将记录保存到数据库中。

validates_attachment_presence :image

关于java - HTTP-POST 图像到 Ruby on Rails 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4845394/

相关文章:

java - 当尝试将 1 到 n 之间的所有数字加在一起时,代码不会返回总和

ruby-on-rails - 如何重写 Rails 路线辅助方法?

ruby-on-rails-3 - 在 Ubuntu 10.04 VM 上安装带有 Rails 3.2.1 的 Postgresql 8.4 时如何创建非系统用户?

ruby-on-rails - Rails3 和回形针

ruby-on-rails-3 - Ruby on Rails,回形针 : "identify" command working in cmd but not in app

ruby-on-rails - Rails Paperclip S3 重命名数千个文件?

java - 通过 TargetDataLine 传输实时音频

java - Java 中的组织结构图/树

java - 关于数组列表中已定义的槽

ruby-on-rails - "rake"可以正常运行我所有的 Cucumber 测试,但是 "cucumber"没有这些步骤