java 客户端上传_将文件从Java客户端上传到HTTP服务器

小编典典

通常,你会java.net.URLConnection用来触发HTTP请求。通常,你还可以multipart/form-data对混合的POST内容(二进制和字符数据)使用编码。单击链接,其中包含信息以及如何组成multipart/form-data请求正文的示例。该规范在RFC2388中有更详细的描述。

这是一个启动示例:

String url = "http://example.com/upload";

String charset = "UTF-8";

String param = "value";

File textFile = new File("/path/to/file.txt");

File binaryFile = new File("/path/to/file.bin");

String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

String CRLF = "\r\n"; // Line separator required by multipart/form-data.

URLConnection connection = new URL(url).openConnection();

connection.setDoOutput(true);

connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

try (

OutputStream output = connection.getOutputStream();

PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);

) {

// Send normal param.

writer.append("--" + boundary).append(CRLF);

writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);

writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);

writer.append(CRLF).append(param).append(CRLF).flush();

// Send text file.

writer.append("--" + boundary).append(CRLF);

writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);

writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!

writer.append(CRLF).flush();

Files.copy(textFile.toPath(), output);

output.flush(); // Important before continuing with writer!

writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

// Send binary file.

writer.append("--" + boundary).append(CRLF);

writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);

writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);

writer.append("Content-Transfer-Encoding: binary").append(CRLF);

writer.append(CRLF).flush();

Files.copy(binaryFile.toPath(), output);

output.flush(); // Important before continuing with writer!

writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

// End of multipart/form-data.

writer.append("--" + boundary + "--").append(CRLF).flush();

}

// Request is lazily fired whenever you need to obtain information about response.

int responseCode = ((HttpURLConnection) connection).getResponseCode();

System.out.println(responseCode); // Should be 200

当你使用诸如Apache Commons HttpComponents Client之类的第三方库时,此代码不太冗长。

某些错误建议表明,Apache Commons FileUpload仅在服务器端有用。你不能在客户端使用它,也不需要它。

2020-03-15

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值