java form 上传文件_用于上传文件的Java HTTP客户端(multipart / form-data)

用于上传文件的Java HTTP客户端(multipart / form-data)

2020-01-10

我正在尝试实现一个HTTP客户端,该客户端发出多部分请求以将文件上载到HTTP服务器. HTML表单有三个输入字段:一个用于用户名,一个用于密码,一个用于文件.服务器端如下所示.

required />required />required />

我的实现如下.

public class MultipartUploader {

private static final String CHARSET = "UTF-8";

private static final String CRLF = "\r\n";

public String httpUpload(String url,String filename,byte[] byteStream)

throws MalformedURLException,IOException {

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

final String boundary = Strings.repeat("-",15) + Long.toHexString(System.currentTimeMillis());

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

connection.setRequestMethod("POST");

connection.setRequestProperty("Connection","Keep-Alive");

connection.setRequestProperty("Cache-Control","no-cache");

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

OutputStream directOutput = connection.getOutputStream();

PrintWriter body = new PrintWriter(new OutputStreamWriter(directOutput,CHARSET),true);

body.append(CRLF);

addSimpleFormData("myuser","myUserName",body,boundary);

addSimpleFormData("mypassword","mySecretPassword",boundary);

addFileData("myfile",filename,byteStream,directOutput,boundary);

addCloseDelimiter(body,boundary);

int responseCode = connection.getResponseCode();

String responseMessage = connection.getResponseMessage();

String payload = CharStreams.toString(new InputStreamReader(connection.getInputStream()));

return payload;

}

private static void addSimpleFormData(String paramName,String wert,PrintWriter body,final String boundary) {

body.append(boundary).append(CRLF);

body.append("Content-Disposition: form-data; name=\"" + paramName + "\"").append(CRLF);

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

body.append(CRLF);

body.append(wert).append(CRLF);

body.flush();

}

private static void addFileData(String paramName,byte[] byteStream,OutputStream directOutput,final String boundary) throws IOException {

body.append(boundary).append(CRLF);

body.append("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + filename + "\"")

.append(CRLF);

body.append("Content-Type: application/octed-stream").append(CRLF);

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

body.append(CRLF);

body.flush();

directOutput.write(byteStream);

directOutput.flush();

body.append(CRLF);

body.flush();

}

private static void addCloseDelimiter(PrintWriter body,final String boundary) {

body.append(boundary).append("--").append(CRLF);

body.flush();

}

}

服务器响应200 OK.我遇到的问题是,不正确地创建了HTTP正文,以便我从服务器获得的响应表明并非所有表单字段都已设置.服务器没有说明它是哪个字段.

所以我的问题是你看到这个代码有什么问题吗?我是否正确创建了多部分请求?

我还尝试使用cURL上传一个文件,并使用以下命令.

cURL -F "myuser=myUserName" -F "mypassword=mySecretPassword" -F "myfile=@/path/to/my/file.zip" "http://abcdef.gh:1234/path/to/uploader"

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值