gpt4 book ai didi

Android:使用json上传图片

转载 作者:太空宇宙 更新时间:2023-11-04 02:12:11 25 4
gpt4 key购买 nike

我正在尝试将位图从我的 Android 应用程序上传到我的 NodeJS 服务器。我的位图有一些数据(Json),必须在同一(POST)消息中上传。我发现可以通过以下方式实现:

  • 多部分表单数据。
  • 在应用端进行图像编码(将图像转换为字符串),在服务器端进行解码。

还有更好的办法吗?有谁熟悉执行此类上传的良好指南吗?

最佳答案

感谢您的回复。这是我为分段上传编写的代码(多个答案的合并)。如果代码有任何问题,请告诉我。

    // create a url object
URL url = new URL(mGlobalUrl + localUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);

conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());

//--start--
//Deal with data
outputStream.writeBytes(twoHyphens + boundary + lineEnd); //key
outputStream.writeBytes("Content-Disposition: form-data; name=\"key\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(value); //value
outputStream.writeBytes(lineEnd);

//Deal with image
outputStream.writeBytes(twoHyphens + boundary + lineEnd); //key
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadFile\"; filename=\"" + "image1" + "\"" + lineEnd);
//outputStream.writeBytes("Content-Type: " + fileMimeType + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);

FileInputStream fileInputStream = new FileInputStream(imagePath); //value
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];

int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);

outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
//--end--

// receive response as inputStream
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
inputStream = new BufferedInputStream(conn.getInputStream());

// convert input stream to string
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
}
else {

Log.d(TAG, "sendPostRequest: Error - input stream is null");
result = "{}";
}
}

fileInputStream.close();
outputStream.flush();
outputStream.close();
conn.disconnect();

对于服务器端(NodeJS),我使用了 Formidable 模块(我不确定它是否已弃用),您可以找到有关此模式的更多信息 here

upload_image = function(req, res, next) {

//create new incoming form object
var form = new formidable.IncomingForm();

//parse the request using formidable module
form.parse(req, function(error, fields, files) {

//check if error occurred during the parse
if (error) {
...some code
return;
}

//save the data received in the request
...some code
});

//check for errors during the transmission
form.on('error', function(err) {
...some code
return;
});

//check for the end of the transmission
form.on('end', function(error, fields, files) {
//save the temporary location of the uploaded image file
var temp_path = this.openedFiles[0].path;

//save the file name of the uploaded image file
var file_name = this.openedFiles[0].name;

//save the file
...some code

//update the database
...some code
};

关于Android:使用json上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41631632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com