gpt4 book ai didi

android - 上传图片到服务器使用base64编码android

转载 作者:行者123 更新时间:2023-11-30 01:19:17 24 4
gpt4 key购买 nike

我正在尝试将图像字符串上传到服务器,但每次我上传时它都会显示一条消息,没有从您这边收到任何信息(表示空字符串)。我还在编码后检查了图像字符串,但仍然无法上传图像字符串。你能帮帮我吗?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

public class UploadImages {
String _responseMain;
private int serverResponseCode;

public String upLoad2Server(String encode) {


HttpURLConnection conn = null;


DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;



try {
Log.d("encodedImage", encode);
URL url = new URL(ConfigImages.UPLOAD_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setChunkedStreamingMode(1024);



conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestMethod("POST");
conn.connect();
conn.connect();



OutputStream outputStream = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(encode);

writer.close();
outputStream.close();

//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

String line = null;
StringBuilder sb = new StringBuilder();

while ((line = bufferedReader.readLine()) != null) {
System.out.println("Uploading............");
sb.append(line);
}

bufferedReader.close();
_responseMain = sb.toString();
System.out.println("Response of Image Upload : " +_responseMain);


} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return _responseMain ;





}
}

最佳答案

您没有发送像这样的参数名称

 {"image_code":"AAARoABQAAAAEAAAEe"}

关于android - 上传图片到服务器使用base64编码android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37368219/

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