Android-Java HTTPCLIENT使用Content-Disposition发送HTTP参数进行图像上传

huangapple go评论87阅读模式
英文:

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");

huangapple
  • 本文由 发表于 2020年4月4日 07:38:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61021958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定