将文本文件从Android上载到Rails会出现内容类型欺骗错误

将文本文件从Android上载到Rails会出现内容类型欺骗错误,android,ruby-on-rails,paperclip,Android,Ruby On Rails,Paperclip,我有一个Android客户端,希望向我的Rails服务器发送一个文件。我正在使用Paperclip Gem for rails,这是我在WebRick控制台中遇到的错误: 于2015-07-23 16:51:20+0800开始发布192.168.63.142的日志 ActiveRecord::SchemaMigration加载0.1ms从schema_迁移中选择schema_迁移。* LogsControllercreate作为HTML进行处理 参数:{log=>{description=>de

我有一个Android客户端,希望向我的Rails服务器发送一个文件。我正在使用Paperclip Gem for rails,这是我在WebRick控制台中遇到的错误:

于2015-07-23 16:51:20+0800开始发布192.168.63.142的日志 ActiveRecord::SchemaMigration加载0.1ms从schema_迁移中选择schema_迁移。* LogsControllercreate作为HTML进行处理 参数:{log=>{description=>description,user\u id=>1,file=>,@original\u filename=bugreport-1hour-head.txt,@content\u type=application/octet stream,@headers=content-Disposition:form-data;name=\log[file]\;filename=\bugreport-1hour-head.txt\\r\n内容类型:application/octet stream\r\n>} 命令::file-b-mime'/tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-lwvwns.txt' [paperclip]内容类型欺骗:文件名bugreport-1hour-head.txt应用程序/来自标题的八位字节流,[]来自扩展名,从文件中发现的内容类型命令:text/plain。请参阅文档以允许这种组合。 0.1ms开始事务 命令::file-b-mime'/tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-1ndzzr1.txt' [paperclip]内容类型欺骗:文件名bugreport-1hour-head.txt应用程序/来自标题的八位字节流,[]来自扩展名,从文件中发现的内容类型命令:text/plain。请参阅文档以允许这种组合。 0.2ms回滚事务 渲染日志/_form.html.erb 16.6ms 布局/应用程序21.6ms内呈现的日志/new.html.erb 在656ms视图中完成200 OK:306.1ms |活动记录:0.7ms

上面说

从文件命令中发现的内容类型:text/plain。请参阅文档以允许这种组合

但我已经在下面的模型中允许了内容类型text/plain

Android代码:

带有回形针值的Rails模型:


我认为这条消息是由内容欺骗的验证检查引起的。 对于回形针v.4,这会产生一个bug[

而对于回形针v.3,它似乎只是抛出了一个弃用警告[

所以要么..在这个文件中使用它

将此添加到初始值设定项以禁用欺骗保护:

配置/初始化器/回形针\媒体\类型\欺骗\检测器\覆盖.rb 或

您可以在config/initializers中指定paperclip.rb文件创建的mime类型


我认为这条消息是由内容欺骗的验证检查引起的。 对于回形针v.4,这会产生一个bug[

而对于回形针v.3,它似乎只是抛出了一个弃用警告[

所以要么..在这个文件中使用它

将此添加到初始值设定项以禁用欺骗保护:

配置/初始化器/回形针\媒体\类型\欺骗\检测器\覆盖.rb 或

您可以在config/initializers中指定paperclip.rb文件创建的mime类型


您的Android客户端以application/octet steam而不是text/plain的形式发送文件,这会导致曲别针混淆,从而导致此异常。您可能必须修复客户端才能使其正常工作。

您的Android客户端以application/octet steam而不是text/plain的形式发送文件,这会导致曲别针混淆,从而导致此异常异常。您可能必须修复客户端才能使其正常工作。

看起来android是以应用程序/八位字节流而不是文本/普通流的形式发送的,这种不匹配会导致异常。伙计,您说得对!我将文件实体编辑为multipartEntity.addPartlog[文件],新的FileBodyfile,text/plain;成功了!请回答,我会接受。看起来android是以应用程序/八位字节流的形式发送的,而不是text/plain,不匹配会导致异常。伙计,你说得对!我将文件实体编辑为multipartEntity.addPartlog[文件],new FileBodyfile,text/plain;有效!请回答,我接受。
 mSendLogs.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url = "http://192.168.63.145:3000/logs";
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
                    "bugreport-1hour-head.txt");
            try {
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(url);
                MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                multipartEntity.addPart("log[description]", new StringBody("Description"));
                multipartEntity.addPart("log[user_id]", new StringBody("1"));
                multipartEntity.addPart("log[file]", new FileBody(file) );
                httppost.setEntity(multipartEntity);

                HttpResponse response = httpclient.execute(httppost);
                String statusCode = response.getEntity().getContent().toString();
                Log.d("Benggal", "http.fr.server: " + statusCode + "Upload Logs");

            } catch (Exception e) {
                Log.e("Benggal",e.toString());
            }
        }
    });
class Log < ActiveRecord::Base
has_attached_file :file
validates_attachment_content_type :file, :content_type => "text/plain"
 # POST /logs
  # POST /logs.json
  def create
    @log = Log.new(log_params)

    respond_to do |format|
      if @log.save
        format.html { redirect_to @log, notice: 'Log was successfully created.' }
        format.text {@log.file.url}
        format.json { render :show, status: :created, location: @log }
      else
        format.html { render :new }
        format.json { render json: @log.errors, status: :unprocessable_entity }
      end
    end
  end
 gem "paperclip", "~> 3.5.3"
require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end
Paperclip.options[:content_type_mappings] = {
  :txt=> 'application/octet-stream'
}