我被要求以这种格式发送报告
Name=tan ah kow
Email=myemail@google.com
Contact=81234567
Block=123
Street=Tampines Central
Description=Dirty water puddle
uploadedfile=a5fa23423-4907098 (BASE64-ENCODED STRING)
index=1
将HTTP标头“Content-Type”设置为“multipart / form-data; boundary = BOUNDARY_STRING”
根据multipart / form-data的HTTP POST规范形成HTTP正文消息 格式是
--BOUNDARY_STRING\r\n
Content-Disposition: form-data; name=\"Name\"\r\n\r\n
tan ah kow\r\n
--BOUNDARY_STRING\r\n
Content-Disposition: form-data; name=\"Email\"\r\n\r\n
myemail@google.com\r\n
...
...
...
--BOUNDARY_STRING\r\n
Content-Dispostion: form-data; name=\"uploadedfile\"; filename=\"android.png\"\r\n
Content-Type: MIME-TYPE\r\n
Content-Transfer-Encoding: binary\r\n\r\n
a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098\r\n
--BOUNDARY_STRING--
如果只上传了1张照片,只需发送一条http帖子,然后设置index = 1。 如果有2张照片,请发送相同数据(姓名,电子邮件,联系方式等)的两个http帖子,但上传文件除外,这应该是第二张照片的数据,并设置index = 2。 如果有3张照片,请发送相同数据(姓名,电子邮件等)的三个http帖子,但上传文件除外,这应该是第三张照片的数据,并设置index = 3.
服务器将处理照片与正确报告的链接。
以下代码将图像发送到服务器但无法正常工作。我希望有人能帮助我。
String urlFEEDBACK = "http://zendragon.sg/tampinestc/report.php";
private class NewFeedbackReportAsyncTask extends AsyncTask<HashMap<String, String>, Void, String> {
@Override
protected void onPreExecute () {
}
@Override
protected String doInBackground(HashMap<String, String>... arrHashMapParameters) {
HashMap<String, String> hashmapInput = arrHashMapParameters[0];
String strPost = "source=android";
Iterator<Entry<String, String>> it = hashmapInput.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>)it.next();
strPost += "&"+(pairs.getKey() + "=" + pairs.getValue());
it.remove();
}
String strFileData = convertPhotoToString(bmp);
String result = null;
try {
result = multipartRequest(urlFEEDBACK, strPost, strFileData);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
return result;
}
@Override
protected void onPostExecute(String result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
showResults();
}
}
此代码用于将位图转换为BASE64-ENCODED字符串
protected String convertPhotoToString(Bitmap theBitMap) {
if(theBitMap == null) {
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
theBitMap.compress(CompressFormat.JPEG, 60, bos);
byte[] array = bos.toByteArray();
return android.util.Base64.encodeToString(array, android.util.Base64.NO_WRAP);
}
这是发送到服务器的代码。
public String multipartRequest(String urlFEEDBACK2, String strPost, String strFileData) throws ParseException, IOException{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String lineEnd = "\r\n";
String boundary = "BOUNDARY_STRING";
String result = "";
try {
URL url = new URL(urlFEEDBACK2);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent",
"Android Multipart HTTP Client 1.0");
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Name\""
+ lineEnd + lineEnd + strName + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Email\""
+ lineEnd + lineEnd + strEmail + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Contact\""
+ lineEnd + lineEnd + strPhoneNo + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Block\""
+ lineEnd + lineEnd + strBLK + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Street\""
+ lineEnd + lineEnd + strStreet + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Description\""
+ lineEnd + lineEnd + strDescription + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"android.png\"" + lineEnd);
outputStream.writeBytes("Content-Type: image/png" + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);
if(bmp!=null) {
//EDITED
outputStream.writeBytes(strFileData);
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
//baos.writeTo(outputStream);
//baos.close();
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens);
inputStream = connection.getInputStream();
result = this.convertStreamToString(inputStream);
// Clean up
inputStream.close();
outputStream.flush();
outputStream.close();
// Log see debug
Log.d(getString(R.string.app_name), "Result: "+ result);
checkResponse(result);
return result;
} catch (Exception e) {
Log.e("MultipartRequest", "Multipart Form Upload Error");
e.printStackTrace();
return "error";
}
}
* 已编辑:* 我似乎没有看到我的代码将图像发送/写入Web服务器的任何问题。希望有人能指出我的错误在这里。我需要尽快解决这个问题,因为我的老板一直在问我。希望有人可以帮助我。任何帮助将不胜感激。提前谢谢。