英文:
Android-Java HTTPCLIENT send http parameters with Content-Disposition image uploading
问题
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");
// Insert additional form parameter
outputStream.writeBytes("--" + boundary + "\r\n");
outputStream.writeBytes("Content-Disposition: form-data; name=\"description\"\r\n");
outputStream.writeBytes("\r\n");
outputStream.writeBytes("my image's description\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");
}
英文:
I have a image uploader function in java using HttpClient and I'm using Content-Disposition for it.
But I'm stuck there I did not realize how can i insert another body form paramaters in this post request.
For example I want to add a paramaters like:
description = "my image's description"
My Code :
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");
}
答案1
得分: 0
我会尝试这样做:
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("--" + boundary + "\r\n");
outputStream.writeBytes("Content-Disposition: form-data; name=\"description\"\r\n");
outputStream.writeBytes("\r\n");
outputStream.writeBytes(description);
outputStream.writeBytes("--" + boundary + "\r\n");
注意:上述代码为翻译后的结果,已经将HTML实体编码(比如"
)还原为正常的字符。
英文:
I'd try doing this:
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("--" + boundary + "\r\n");
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + "description" + "\"" + "\r\n");
outputStream.writeBytes("\r\n");
outputStream.writeBytes(description);
outputStream.writeBytes("--" + boundary + "\r\n");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论