我正在尝试将图像字符串上传到服务器,但每次我上传它都会发出一条消息,没有从你身边收到(意味着空字符串)。我还在编码后检查了图像字符串,但仍然无法上传图像的字符串。你能帮我吗?
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 ;
}
}
答案 0 :(得分:1)
您没有发送像
这样的参数名称 {"image_code":"AAARoABQAAAAEAAAEe"}
答案 1 :(得分:0)
我找到了答案: 我使用了json格式,但我没有正确指定json的名称,所以代码应该是
package Uploadvideo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.InputStream;
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 org.json.JSONObject;
import android.util.Log;
public class UploadImages {
String _responseMain;
private int serverResponseCode;
JSONObject jsonObject;
public String upLoad2Server(String Encode) {
try {
Log.d("Vicky", "encodedImage = " + Encode);
jsonObject = new JSONObject();
jsonObject.put("image_code", Encode);
String data = jsonObject.toString();
String yourURL = "URL Name";
URL url = new URL(yourURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setFixedLengthStreamingMode(data.getBytes().length);
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(data);
Log.d("Vicky", "Data to php = " + data);
writer.flush();
writer.close();
out.close();
connection.connect();
InputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(
in, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
in.close();
String result = sb.toString();
Log.d("Vicky", "Response from php = " + result);
//Response = new JSONObject(result);
connection.disconnect();
} catch (Exception e) {
Log.d("Vicky", "Error Encountered");
e.printStackTrace();
}
return null;
}
}