通过HTTPS帖子将文件发送到远程



我需要使用SSL向PHP Web服务发送Zipped数据库(有效载荷〜10 MB)。

该代码工作正常数周,但是我得到了ssl broken pipe exception(SSL损坏的管道)。我看不出任何错误的原因 - 我没有更改代码,并且服务器配置仍然相同(IIS可以处理SSL的数据传输)。

目前,我使用java.net.HttpsURLConnection(下面的代码) - 您建议我在将文件从Android发送到Web服务时使用哪种替代方案?看来该帖子没有在设备上处理。

预先感谢

try {
    final File compressedDb = new File(Environment.getExternalStorageDirectory() + "/" + Const.TMP_DIR + "/database.zip");
    String sourceFileUri = compressedDb.getPath();
final long compressedDbSize = compressedDb.length();
float optimized = ((float) _dbSize / (float) compressedDbSize) * 100f;
int dbKb = (int) (((float) _dbSize) / 1024f);
int dbCompKb = (int) ((float) compressedDbSize / 1024f);
compressionInfo = getString(R.string.compression) + ": " +
        dbKb + "kB " + getString(R.string.to) + " " + dbCompKb + "kB" + "<br>" +
        getString(R.string.saved) + ": " + (int) optimized + "%" + "<br>";
Log.i(TAG, "DB name=" + compressedDb.getName() + " size=" + compressedDb.length() / 1024 + "kB.");
HttpsURLConnection connection;
DataOutputStream dos;
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
File sourceFile = new File(sourceFileUri);
//                int maxBufferSize = 1024 * 1024;
int maxBufferSize = (int) sourceFile.length();
if (sourceFile.isFile()) {
    try {
        // open a URL connection to the Servlet
        FileInputStream fileInputStream = new FileInputStream(sourceFile);
        url = new URL(params[0]);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoInput(true); // Allow Inputs
connection.setUseCaches(false); // Don't use a Cached Copy
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("the_database", sourceFileUri);
        dos = new DataOutputStream(connection.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name="the_database";" +
                "filename="" + sourceFileUri + """ + lineEnd);
        dos.writeBytes(lineEnd);
        // create a buffer of maximum size
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        // read file and write it into form...
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        int bytesTotal = bytesAvailable;
        Log.i(TAG, "maxBuffer=" + maxBufferSize +
                " | bytesTotal=" + bytesTotal + " | bytesAvailable=" + bytesAvailable);
        while (bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            int progress = 100 - (int) (((float) bytesAvailable * 100 / (float) bytesTotal));
            publishProgress("" + progress);
        }
        // send multipart form data necessary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // Responses from the server (code and message)
        int serverResponseCode = connection.getResponseCode();
        final String responseMessagePostDb = connection.getResponseMessage();
        fileInputStream.close();
        dos.flush();
        dos.close();
        if (responseMessagePostDb != null) {
            responseStrings.add("<br>");
            responseStrings.add("<b><u>" + getString(R.string.optimisation_string) + "</u></b>" + "<br>");
            responseStrings.add(params[1]);
            responseStrings.add("<b><u>" + getString(R.string.optimisation_database) + "</u></b>" + "<br>");
            responseStrings.add(compressionInfo);
            String response = (responseMessagePostDb.equals("OK") ? ("<font color="" + GREEN + "">PASSED</font>") : "FAILED");
            responseStrings.add(getString(R.string.server_response) + ": " + response);
            return responseStrings;
            }
        } catch (Exception e) {
            Log.e(TAG, "doInBackground: " + e.getMessage());
            responseStrings.add("FAILED2"); // <- this exception is thrown
            responseStrings.add(e.getMessage());
            return responseStrings;
        }
    }
} catch (Exception ex) {
    Log.e(TAG, "doInBackground: " + ex.getMessage());
    responseStrings.add("FAILED3");
    responseStrings.add(ex.getMessage());
    return responseStrings;
}
Log.e(TAG, "doInBackground: " + "FAILED4");
responseStrings.add("FAILED4");
responseStrings.add("Unexpected Error...");
return responseStrings;

如果您的代码到达这一点,则在逻辑上,除非您的代码中有种族条件,否则您不会看到不一致的结果代码。换句话说,如果服务器决定执行连接超时,则您的代码似乎无缘无故地开始失败。即使是涉及SSL损坏管道异常的问题的链接也意味着原因是服务器,而不是客户端。

我的建议是尝试提出各种故障原因,涉及服务器做某件事,然后通过更改代码来测试每个理论。换句话说,如果问题是超时的问题,请在操作中计时并通过网络发送较小的文件并验证是否有效。如果确实如此,您有问题。如果不是这样,请继续进行下一个理论。

不幸的是,上述代码不是一个问题,这使此问题无法解决,而又不了解更多细节。

相关内容

最新更新