将文件从 Java 客户端上传到 HTTP 服务器
我想将一些文件上传到 HTTP 服务器。基本上我需要的是带有一些参数和文件的对服务器的某种 POST 请求。我见过仅上传文件的示例,但没有找到如何传递其他参数。
这样做最简单且免费的解决方案是什么?有人有我可以学习的文件上传示例吗?我已经在谷歌上搜索了几个小时,但是(也许这只是那些日子之一)无法找到我需要的东西。最好的解决方案是不涉及任何第三方类或库的解决方案。
I'd like to upload a few files to a HTTP server. Basically what I need is some sort of a POST request to the server with a few parameters and the files. I've seen examples of just uploading files, but didn't find how to also pass additional parameters.
What's the simplest and free solution of doing this? Does anyone have any file upload examples that I could study? I've been googling for a few hours, but (maybe it's just one of those days) couldn't find exactly what I needed. The best solution would be something that doesn't involve any third party classes or libraries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您通常会使用
java.net.URLConnection
来触发 HTTP 请求。您通常还可以使用multipart/form- data
混合 POST 内容(二进制和字符数据)的编码。单击该链接,它包含如何组成multipart/form-data
请求正文的信息和示例。 RFC2388 中更详细地描述了该规范。下面是一个启动示例:
当您使用像 Apache Commons HttpComponents Client 这样的第 3 方库时,此代码会更简洁。
Apache Commons FileUpload 正如一些人错误地建议的那样,仅对服务器端感兴趣。您不能在客户端使用也不需要它。
另请参阅
You'd normally use
java.net.URLConnection
to fire HTTP requests. You'd also normally usemultipart/form-data
encoding for mixed POST content (binary and character data). Click the link, it contains information and an example how to compose amultipart/form-data
request body. The specification is in more detail described in RFC2388.Here's a kickoff example:
This code is less verbose when you use a 3rd party library like Apache Commons HttpComponents Client.
The Apache Commons FileUpload as some incorrectly suggest here is only of interest in the server side. You can't use and don't need it at the client side.
See also
以下是使用 Apache HttpClient 的方法(此解决方案适用于那些不介意使用第 3 方的人)图书馆):
Here is how you would do it with Apache HttpClient (this solution is for those who don't mind using a 3rd party library):
单击链接获取示例文件上传 clint java 与 apache HttpComponents
http://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
和库 downalod链接
https://hc.apache.org/downloads.cgi
使用 4.5.3.zip 它是 中工作正常..
在我的代码和工作代码
click link get example file upload clint java with apache HttpComponents
http://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
and library downalod link
https://hc.apache.org/downloads.cgi
use 4.5.3.zip it's working fine in my code
and my working code..
以下是使用 Java 11 的 java.net.http 包执行此操作的方法:
使用以下 MimeMultipartData:
Here is how you could do it with Java 11's java.net.http package:
With the following MimeMultipartData:
我建议使用 Apache http 类而不是 Vanilla Java。这是一个兼容 Java 8 的简单解决方案:
https://company.com/jobs/status?jobId=876&apiKey= 123
I suggest to use Apache http classes instead of Vanilla Java. This is a Java 8 compatible simple solution:
https://company.com/jobs/status?jobId=876&apiKey=123
并使用
And use
<form enctype="multipart/form-data">
and use<input type="file">
in the html这可能取决于您的框架。 (对于他们每个人都可以存在一个更简单的解决方案)。
但要回答你的问题:有很多外部库这个功能。请查看此处如何使用 apache commons fileupload。
It could depend on your framework. (for each of them could exist an easier solution).
But to answer your question: there are a lot of external libraries for this functionality. Look here how to use apache commons fileupload.