1

我被指示以这种格式发送报告

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 FORMAT IS 的 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 post,并设置 index=1。如果有 2 张照片,则发送两个相同数据(姓名、电子邮件、联系方式等)的 http post,但uploadfile 应该是第二张照片的数据,并设置 index=2。如果有3张照片,则发送三个相同数据(姓名,电子邮件等)的http post,除了uploadfile,应该是第三张照片的数据,并设置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";
    }
}

*已编辑:* 我的代码似乎看不到任何问题,以将图像发送/写入网络服务器。希望任何人都可以在这里指出我的错误。我需要尽快解决这个问题,因为我的老板一直在问我。希望任何人都可以在这里帮助我。任何帮助将不胜感激。提前致谢。

4

0 回答 0