java 给 Apache HttpPost 添加参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3367870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:31:47  来源:igfitidea点击:

Add parameters to Apache HttpPost

javapostparametershttpclient

提问by Alex

I'm trying to send a file to a Servlet. Along with this file, I also have to send some parameters (i.e. name/id, date and a few others). I'm using HttpClient on client-side and ServerFileUpload on server-side.

我正在尝试将文件发送到 Servlet。除了这个文件,我还必须发送一些参数(即名称/ID、日期和其他一些参数)。我在客户端使用 HttpClient,在服务器端使用 ServerFileUpload。

This is the client-side code: ...

这是客户端代码:...

String url = "http://localhost:8080/RicezioneServlet/RicezioneServlet";
HttpClient httpclient = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(url);
MultipartEntity mpe = new MultipartEntity();
//I'm sending a .zip file
ContentBody cb = new FileBody(fileToSend,"application/zip");
mpe.addPart("file", cb);
postMethod.setEntity(mpe);
HttpResponse resp = httpclient.execute(postMethod);
HttpEntity respEntity = resp.getEntity();
System.out.println(resp.getStatusLine());

...

...

on the server side, we've got:

在服务器端,我们有:

ServletFileUpload sup = new ServletFileUpload();
FileItemIterator it = sup.getItemIterator(request);
FileItemStream item = it.next();
InputStream ios = item.openStream();
//read from ios and write to a fileoutputstream.

Now, I don't know how to add the aforementioned parameters to the request... I tried to use a StringBody and to add it to the MultiPartEntity, but I get a NullPointerException at:

现在,我不知道如何将上述参数添加到请求中……我尝试使用 StringBody 并将其添加到 MultiPartEntity,但我在以下位置收到 NullPointerException:

String author = request.getParameter("author");

which means that the parameter isn't seen as a parameter, maybe?

这意味着参数不被视为参数,也许?

The only was I got it working was setting these parameters as Headers (setHeader and getHeader), but this is not an option.

我唯一让它工作的是将这些参数设置为标题(setHeader 和 getHeader),但这不是一个选项。

Any advice? Or maybe can you redirect me to a fullexample of file+parameter upload?
Thanks,
Alex

有什么建议吗?或者您能否将我重定向到文件+参数上传的完整示例?
谢谢,
亚历克斯

回答by YoK

Try using similar code as pasted here :

尝试使用与此处粘贴的类似代码:

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

FileBody bin = new FileBody(new File(fileName));
StringBody comment = new StringBody("Filename: " + fileName);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

You will also need to add the external jar apache-mime4j-0.6.jar (org.apache.james.mime4j) otherwise

您还需要添加外部 jar apache-mime4j-0.6.jar (org.apache.james.mime4j) 否则

reqEntity.addPart("bin", bin);

would not compile.

不会编译。

回答by lishunli

if you use servlet 3.0, you can try add @MultipartConfig onto your servlet.

如果您使用 servlet 3.0,您可以尝试将 @MultipartConfig 添加到您的 servlet。