Java Apache HttpClient上传文件时出错(Java Apache HttpClient error uploading files)

我想使用HttpClient 4.3.1上传文件。 我需要添加以形成多部分输入流(以控制上载进度。)而不是File对象。 这是我的代码:

        byte[] dump = ...;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(SENDLOG_URL);      

        MultipartEntityBuilder meb = MultipartEntityBuilder.create();
        meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        // THIS WORKS!!  meb.addBinaryBody("log", new File("C:\\temp.zip"), ContentType.APPLICATION_OCTET_STREAM,"log.zip");
        InputStream is=new FileInputStream("C:\\temp.zip")
        meb.addBinaryBody("log", is, ContentType.DEFAULT_BINARY, "log.zip");            
        httppost.setEntity(meb.build());
        HttpResponse resp=httpclient.execute(httppost);
        is.close();

使用方法MultipartEntityBuilder.addBinaryBody(Strin,File)它可以工作,但是使用MultipartEntityBuilder.addBinaryBody(Strin,InputStream)它不起作用。

java客户端不会抛出任何错误,但这是远程服务器(Apache)的答案:

HTTP / 1.1 400错误请求 [日期:星期四,2013年11月28日09:46:08 GMT,服务器:Apache / 2.4.6(Unix)mod_fcgid / 2.3.7,内容长度:226,连接:关闭,内容类型:text / html; 字符集= ISO-8859-1]

为什么新的File()和新的FileInputStream()之间存在这种奇怪的行为?


I want to upload a file using HttpClient 4.3.1. I need to add to form multipart an inputstream (to control upload progress.) instead of a File object. This is my code:

        byte[] dump = ...;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(SENDLOG_URL);      

        MultipartEntityBuilder meb = MultipartEntityBuilder.create();
        meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        // THIS WORKS!!  meb.addBinaryBody("log", new File("C:\\temp.zip"), ContentType.APPLICATION_OCTET_STREAM,"log.zip");
        InputStream is=new FileInputStream("C:\\temp.zip")
        meb.addBinaryBody("log", is, ContentType.DEFAULT_BINARY, "log.zip");            
        httppost.setEntity(meb.build());
        HttpResponse resp=httpclient.execute(httppost);
        is.close();

Using the method MultipartEntityBuilder.addBinaryBody(Strin,File) it works, but using MultipartEntityBuilder.addBinaryBody(Strin,InputStream) it doesn't work.

The java client doesn't throw any error, but this is the answer of remote server (Apache):

HTTP/1.1 400 Bad Request [Date: Thu, 28 Nov 2013 09:46:08 GMT, Server: Apache/2.4.6 (Unix) mod_fcgid/2.3.7, Content-Length: 226, Connection: close, Content-Type: text/html; charset=iso-8859-1]

Why this strange behaviour between new File() and new FileInputStream()?


原文:https://stackoverflow.com/questions/20262769
2022-09-29 17:09

满意答案

你需要一个OUTTER JOIN,这可以通过简单地添加.DefaultIfEmpty()来完成。

from a in context.Module_Articles_Articles
join c in context.Module_Articles_Categories on a.CategoryID equals c.CategoryID into ca
from c in cs.DefaultIfEmpty()
join co in context.Module_Articles_Comments on a.ArticleID equals co.ArticleID into com
from co in com.DefaultIfEmpty()
where a.IsDraft == false
orderby a.ArticleID descending
select new ...

you want an OUTTER JOIN, which can be accomplished in a query like this by simply adding .DefaultIfEmpty()

from a in context.Module_Articles_Articles
join c in context.Module_Articles_Categories on a.CategoryID equals c.CategoryID into ca
from c in cs.DefaultIfEmpty()
join co in context.Module_Articles_Comments on a.ArticleID equals co.ArticleID into com
from co in com.DefaultIfEmpty()
where a.IsDraft == false
orderby a.ArticleID descending
select new ...

相关问答

更多

空表上的LEFT JOIN结果为NULL(LEFT JOIN on empty table NULLs results)

试试这个: SELECT alias.name alias, team.name team, sid.steam_id steam_id, (SUM(IFNULL(ws1.kills,0)) + SUM(IFNULL(ws2.kills,0)) + SUM(IFNULL(spnr.kills,0))) kills_total FROM pickup JOIN player ON player.pickup_id = pickup.id ...

JOIN EACH没有返回结果(JOIN EACH not returning result)

这是Firefox上BigQuery Web UI中的一个错误。 对长期工作的轮询工作不正常,因此任何超过2分钟的工作都表现出上述行为。 我们已经修复了这个错误,修复程序将在下周推出。 This was a bug in the BigQuery web UI on Firefox. Polling for long-running jobs was not working correctly, so any job that ran for more than 2 minutes exhibit...

SQL Join问题,如果第二个表为空则返回null(SQL Join question, return null if second table is empty)

使用聚合函数(在这种情况下为AVG() )时,需要使用非聚合字段指定GROUP BY子句,例如 GROUP BY p.id, p.name 要确保所有project引用都存在,无论连接的等级如何,请使用LEFT JOIN 。 When using an aggregate function (AVG() in this case), you need to specify a GROUP BY clause with the non-aggregate fields, eg GROUP BY p...

LINQ如果第二个或第三个表为空,则加入不返回结果(LINQ Join not returning results if second or third table empty)

你需要一个OUTTER JOIN,这可以通过简单地添加.DefaultIfEmpty()来完成。 from a in context.Module_Articles_Articles join c in context.Module_Articles_Categories on a.CategoryID equals c.CategoryID into ca from c in cs.DefaultIfEmpty() join co in context.Module_Articles_Comme...

如何加入并获得4个结果(How to join and get 4 results)

你的问题似乎是在WHERE 。 这个说法: Select A.Artnr, A.grp, A.name, B.eigenschap, B.waarde from A left join B on B.Artnr = A.Artnr 会返回第3条,这是 3 FlowerC NameC NULL NULL 在此之后,您会筛选eigenschap = 'color' ,这会从最终结果集中移除该行,因为eigenschap为NULL 。 将该条件移至连接条件: Select A...

从第二个表返回不同的值时连接表(Join tables while returning distinct values from the second table)

你得到的结果是正确的。 您正在获取列analyzeid和fileid的catersian product 。 您应该只select一列来避免这种情况。 但是,在连接表时尝试使用ANSI-SQL join语法: select t1.id , t1.name , t1.date , t1.analysisid , NULL as fileid from Table_1 t1 JOIN Table_2 t2 ON t2.id = t1.id and t2.name = t1.name and t2.da...

内部联接返回没有结果(Inner Join returning no results)

查询有一个错误,因为您的SELECT slug是不明确的。 您的列slug出现在所有表中,因此MySQL不知道要返回哪一列。 你需要这样做 SELECT `highlight_table`.`slug` from `highlight_table` 这将告诉MySQL只返回highlight_table的slug列。 然后你应该只获得1行蓝色,因为蓝色存在于所有三个表中。 更改为文章和用户表的LEFT JOIN将返回2个结果(绿色和蓝色),因为INNER JOIN基本上用作AND, LEFT ...

SUM表中的值,该表通过第三个表链接到第二个表(SUM a value from a table which is linked to a second table via a third table)

您的查询逻辑非常接近。 你需要GROUP BY : SELECT sj.ID, SUM(c.Size) as Total_Size FROM Sub_Jobs sj INNER JOIN Cargo_Assignment ca ON ca.Sub_Job_ID = sj.ID INNER JOIN Cargo c ON c.ID = ca.Cargo_ID GROUP BY sj.ID; 请注意,您不需要JOIN到Sub_Jobs ,因为该字段位于Carg...

RIGHT JOIN返回所有NULL结果(RIGHT JOIN is returning all NULL results)

我认为正确的加入应该是内联。 请尝试以下查询: SELECT vcc.name, vcc.quantity, vcc.custom_message, vfd.value FROM vxu_4_wpsc_cart_contents AS vcc INNER JOIN vxu_4_wpsc_submited_form_data as vfd ON vcc.purchaseid = vfd.log_id WHERE vfd.form_i...

MySQL内连接不返回结果(MySQL inner join not returning results)

尝试这个: SELECT * FROM table1 t1 INNER JOIN table2 t2 ON (t1.name like CONCAT('%', trim(t2.Last_Name), '%') AND t1.name like CONCAT('%', trim(t2.First_Name), '%') ) Try this: SELECT * FROM table1 t1 INNER JOIN table2 t...

相关文章

更多

HttpClient 上传文件

我们使用MultipartEntityBuilder创建一个HttpEntity。 当创建构建器时,添 ...

Hadoop Java程序-files功能测试

之前一直用Hadoop streaming方式,-file功能非常实用,可以动态上传文件,例如一些配置 ...

httpclient get请求

httpclient get进行get请求步骤: 1、创建Httpclient对象 HttpCli ...

HttpClient DELETE请求示例

本教程演示如何使用Apache HttpClient 4.5创建Http DELETE请求。 HTTP ...

HttpClient PUT请求示例

本教程演示如何使用Apache HttpClient 4.5发出Http PUT请求。 HTTP PU ...

flume写入Hadoop hdfs报错 Too many open files

故障现象: [Hadoop@dtydb6 logs]$ vi hadoop-hadoop-datano ...

httpClient快速入门

本示例是基于HttpClient 4.3,示例比较简单,就是请求http://www.656463.c ...

httpclient post 请求

httpclient post请求与get请求的区别主要是httpclient.execute对象 ...

HttpClient CacheConfig缓存处理示例

是如何设置基本缓存HttpClient的简单示例。 按照配置,它将存储最多3000个缓存对象,其中每个 ...

httpclient 带参数 get 请求

httpclient带参数get请求和默认get请求的主要区别是要初始化HttpGet的方式 默认的g ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本