Java 在android中使用HttpURLConnection将带有参数的图像发送到服务器
我正试图通过我的android应用程序将两张图片和一些参数上传到服务器上。在网上搜索并遵循和以及其他来源的说明后,我得到了以下代码:Java 在android中使用HttpURLConnection将带有参数的图像发送到服务器,java,php,android,post,httpurlconnection,Java,Php,Android,Post,Httpurlconnection,我正试图通过我的android应用程序将两张图片和一些参数上传到服务器上。在网上搜索并遵循和以及其他来源的说明后,我得到了以下代码: String boundary = "***" + System.currentTimeMillis() + "***"; String twoHyphens = "--"; String crlf = "\r\n"; String output = ""; try { HttpURLConnection httpUrlConnection
String boundary = "***" + System.currentTimeMillis() + "***";
String twoHyphens = "--";
String crlf = "\r\n";
String output = "";
try {
HttpURLConnection httpUrlConnection = null;
URL url = new URL(myUrl);
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
httpUrlConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
// Convert and add first image
ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
params[0].compress(Bitmap.CompressFormat.JPEG, 100, bao1);
byte[] ba1 = bao1.toByteArray();
request.writeBytes("Content-Disposition: form-data; name=\"image1\";filename=\"image1\"" + crlf);
request.writeBytes(crlf);
request.write(ba1);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + crlf);
// Convert and add second image
ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
params[1].compress(Bitmap.CompressFormat.JPEG, 100, bao2);
byte[] ba2 = bao2.toByteArray();
request.writeBytes("Content-Disposition: form-data; name=\"image2\";filename=\"image2\"" + crlf);
request.writeBytes(crlf);
request.write(ba2);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"username\"" + crlf);
request.writeBytes(crlf);
request.writeBytes(username);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens);
request.writeBytes("Content-Disposition: form-data; name=\"datestr\"" + crlf);
request.writeBytes(crlf);
request.writeBytes(timeStampString);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens);
request.flush();
request.close();
int responseCode = httpUrlConnection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream, Charset.forName("UTF-8")));
String line;
while ((line = responseStreamReader.readLine()) != null) {
output = line;
Log.d(TAG, line);
}
responseStreamReader.close();
}
httpUrlConnection.disconnect();
if (output == "") {
httpResultsReturned = false;
} else {
httpResultsReturned = true;
}
} catch (ProtocolException e) {
e.printStackTrace();
return "failed";
} catch (MalformedURLException e) {
e.printStackTrace();
return "failed";
} catch (IOException e) {
e.printStackTrace();
return "failed";
}
在服务器端,我尝试按如下方式访问数据:
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$image1 = $_FILES['image1']['name'];
$image2 = $_FILES['image2']['name'];
$datestr= $_POST['datestr'];
$username= $_POST['username'];
}
?>
最终,两个图像都成功传输,但我无法发送/接收额外参数。我正确地收到了响应,但是在所有的php代码中(在这个问题中我省略了一些部分),似乎没有发送/接收任何参数
毫无疑问,AndroSco共享了为他工作的解决方案,但在他的php文件中,他似乎只访问图像,而不访问参数
由于我在这个领域没有太多经验,我相信可能有一些非常明显的事情我做得不对/根本没有做
如有任何建议,将不胜感激
谢谢 在经历了很多挫折之后,我发现了代码中的错误。在传输的消息中导入了两个图像之后,当我想要导入其他参数时,我错误地写入了边界。而不是添加以下内容:
request.writeBytes(twoHyphens + boundary + crlf);
最后我写了一行新词:
request.writeBytes(twoHyphens + boundary + twoHyphens);
在行尾添加两个连字符
将
两个连字符
替换为crlf
后,一切正常 将位图直接压缩到dataoutputstream。您不需要中间bytearrayoutputstream。嗨!谢谢你的建议!我会的!虽然这似乎不会影响我的问题!