Я пытаюсь реализовать HTTP-клиент, который отправляет составные запросы на загрузку файлов на HTTP-сервер. Форма HTML имеет три поля ввода: одно для имени пользователя, одно для пароля и одно для файла. Серверная часть выглядит следующим образом.
<html>
<head>
<title>Uploader</title>
</head>
<body>
<div id="header">
<h1>Uploader</h1>
</div>
<div id="content">
<form id="uploadformular" action="upload" method="post"
enctype="multipart/form-data" accept-charset="utf-8">
<div class="block">
<label for="user">Username</label> <input type="text" id="user"
name="myuser" required />
</div>
<div class="block">
<label for="password">Password</label> <input type="password" id="pin"
name="mypassword" required />
</div>
<div class="block">
<label for="file">ZIP File</label> <input type="file" id="file"
name="myfile" required />
</div>
<div>
<input type="submit" value="Upload" />
</div>
</form>
</div>
</body>
</html>
Моя реализация выглядит следующим образом.
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", body, boundary);
addFileData("myfile", filename, byteStream, body, 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, String filename, byte[] byteStream, PrintWriter body,
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"
-vvv --trace-ascii
(если я правильно помню). Затем выгрузите то, что вы пишете вPrintWriter
, в строку и сравните ее с запросом на завивку байт за байтом. Самый верный способ, конечно, использовать какой-нибудь сетевой сниффер, например Wireshark, чтобы посмотреть, что происходит в сети. - person Devstr   schedule 23.02.2018