仅来自android的特定网络上的图像上传失败

时间:2015-01-26 08:58:47

标签: java android networking image-uploading

我们正在努力从Android设备上传图片到我们的服务器。除了我们当前使用的一个特定网络外,图像在使用移动数据和Wi-Fi时在服务器中成功上传。这个新的网络设置不允许从客户端传递任何数据,其MTU大小超过1460.因此,来自Android设备的所有数据包都被拒绝,无法到达服务器。我们的iOS应用程序可以使用相同的网络成功将图像上传到同一服我们通过跟踪发现,如果没有成功发送一个数据包,iOS会减少数据包或块大小。但Android设备的表现并不像那样。我们还发现一些社交网络Android应用程序可以通过我们的网络成功上传图像。有些人可能会说用正确的设置来改变我们的网络,但是我们不想这样做,好像我们可以有一个有缺陷的网络,其他一些人也可以有,我们的Android应用程序将无法正常工作。

我们到目前为止所尝试的工作(除了新网络外所有工作都有效)如下: 我们首先使用的主要代码:

private boolean uploadImage(String fileLoc, String title){
    try {
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        URL connectURL;

        connectURL = new URL(baseUrlString); // our server url to upload image

        FileInputStream fileInputStream = new FileInputStream(fileLoc);
            // ------------------ CLIENT REQUEST
            // Open a HTTP connection to the URL
            HttpURLConnection conn = (HttpURLConnection) connectURL
                    .openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);

            //conn.setChunkedStreamingMode(512);

            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            //conn.setFixedLengthStreamingMode(137931);             

            DataOutputStream dos = new DataOutputStream(
                    conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);

            dos.writeBytes("Content-Disposition: form-data; name=\""
                    + param1 + "\"" + lineEnd
                    + lineEnd
                    + title
                    + lineEnd);
            dos.writeBytes(twoHyphens + boundary + lineEnd);


            dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
                    + fileLoc
                    + "\""
                    + lineEnd);
            dos.writeBytes(lineEnd);

            // create a buffer of maximum size
            int bytesAvailable = fileInputStream.available();
            int maxBufferSize = 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];

            // read file and write it into form...

            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // close streams
            fileInputStream.close();                
            dos.flush();
            InputStream is = conn.getInputStream();             
            // retrieve the response from server
            int ch;

            StringBuffer b = new StringBuffer();
            while ((ch = is.read()) != -1) {
                b.append((char) ch);
            }
            String response;
            response = b.toString().trim();
            dos.close();

            // here some work with response

            return sucs;


    } catch (Exception e) {
        e.printStackTrace();

    }
    return false;
}

这里我们也试过

conn.setChunkedStreamingMode(512);

conn.setRequestProperty("Transfer-Encoding", "chunked");

限制块大小,但它被忽略,因为文档说它只是一个提示。

我们也试过

conn.setFixedLengthStreamingMode(calculatedValueOfbytesForSameImage);

但失败了。

然后我们尝试使用Multipart和Socket实现Web中的其他方法:

  1. https://stackoverflow.com/a/16529053/1246639
  2. https://stackoverflow.com/a/19188010/1246639
  3. https://stackoverflow.com/a/22803149/1246639
  4. https://groups.google.com/forum/#!msg/android-developers/SZ5MzO6wr6I/Ol4ZL1Au3U0J
  5. 除我们的网络外,所有方法都有效。还尝试将Multipart与ByteArrayOutputStream组合为https://stackoverflow.com/a/10225966/1246639。但仍然无法正常工作。

    是否有其他方法可用于使用其他类(如InterfaceAddress或本机支持)控制块大小,或者我们是否在上面尝试的方法中遗漏了某些内容?感谢.....

0 个答案:

没有答案