栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将文件从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 = "rn"; // 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
仅在服务器端有用。你不能在客户端使用它,也不需要它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号