У меня есть функция загрузки изображений в java с использованием HttpClient, и я использую Content-Disposition для нее. Но я застрял там, я не понял, как я могу вставить другие параметры формы тела в этом посте запроса. Например, я хочу добавить такие параметры как:
description = "my image's description"
Мой код:
File file = new File(filepath);
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(UPLOAD_SERVER);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes("--" + boundary + "\r\n");
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + "img_upload" + "\"; filename=\"" + q[idx] +"\"" + "\r\n");
outputStream.writeBytes("Content-Type: image/jpeg" + "\r\n");
outputStream.writeBytes("Content-Transfer-Encoding: binary" + "\r\n");
outputStream.writeBytes("\r\n");
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, 1048576);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while(bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, 1048576);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes("\r\n");
outputStream.writeBytes("--" + boundary + "--" + "\r\n");
inputStream = connection.getInputStream();
int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
inputStream.close();
connection.disconnect();
fileInputStream.close();
outputStream.flush();
outputStream.close();
return response.toString();
} else {
throw new Exception("Non ok response returned");
}