通过post将图像发送到PHP服务器
我正在尝试将图像从手机图库发送到服务器。 但是我得到了空的$u文件和空的$u帖子通过post将图像发送到PHP服务器,php,android,Php,Android,我正在尝试将图像从手机图库发送到服务器。 但是我得到了空的$u文件和空的$u帖子 new Thread(new Runnable() { @Override public void run() { try { URL url=new URL("server_url"); HttpURLConnec
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("server_url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=");
conn.setReadTimeout(35000);
conn.setConnectTimeout(35000);
// directly let .compress write binary image data
// to the output-stream
OutputStream os = conn.getOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(in));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null)
stringBuilder.append(line).append("\n");
responseStreamReader.close();
String response = stringBuilder.toString();
Log.d("WEDDING","response "+response);
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
我应该如何构造发送图像的POST请求。
我也尝试过使用loopj库,但这没有帮助
谢谢大家! 我没有在安卓系统上做过,但在其他移动平台和应用程序上做过。您需要对图像进行base64编码,将其设置为服务器的post变量,然后在php服务器上对其进行base64解码并将数据保存到文件中。然后,您的服务器上有您的映像。我也遇到了这个问题。经过大量研究,我发现了页面内容类型的问题。我个人建议您首先检查php文件的内容类型。。。!如果不介意的话,你能分享一下工作方法吗?通过它,我可以诊断问题,只向服务器发送json请求(而不是图像)。。。!