如何在Android中恢复文件上传过程?

Jig*_*iya 6 android network-programming

我正在使用此代码在服务器上上传文件,但我想要这样的功能:如果由于网络丢失或任何其他中断而停止,那么它不应该从第二次开始上传.服务器的响应也是可自定义的.它有可能在Android?我应该用什么方法来做这件事?请指导我.如果可能的话,请告诉我任何样本代码.

谢谢!

public String uploadFile(InputStream is) {
    HttpURLConnection conn;
    String jsonResponse = "";
    int streamSize = 0;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesAvailable, bufferSize;
    byte[] buffer;

    String urlString = "*******<My URL>*******";
    try {
        // ------------------ CLIENT REQUEST
        // FileInputStream fileInputStream = new FileInputStream(new
        // File(selectedPath) );

        // open a URL connection to the Servlet

        URL url = new URL(urlString);
        // Open a HTTP connection to the URL
        conn = (HttpURLConnection) url.openConnection();
        // Allow Inputs
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);
        // Don't use a cached copy.
        conn.setUseCaches(false);

        conn.setChunkedStreamingMode(0);
        // Use a post method.
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);
        dos = new DataOutputStream(conn.getOutputStream());

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

        // create a buffer of maximum size
        bytesAvailable = is.available();
        streamSize = bytesAvailable;
        bufferSize = 2048;
        buffer = new byte[bufferSize];
        // read file and write it into form...
        int i = 0;
        int length = 0;
        while ((length = is.read(buffer, 0, bufferSize)) > 0) {
            if (isCancelled()) {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
                return null;
            }
            dos.write(buffer, 0, length);
            bytesAvailable = is.available();

            // bufferSize = Math.min(bytesAvailable, maxBufferSize);

            publishProgress(streamSize, bytesAvailable);
            // Log.v("Progress",""+streamSize+" : "+bytesAvailable);
        }
        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"data[authentication][email]\""
                + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(username);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"data[authentication][password]\""
                + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(enc_password);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"method\""
                + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes("uploadWheezeFile");
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // close streams
        Log.e("Debug", "File is written");

        // Log.v("Conn status", "Disconnected");

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        Log.e("Debug", "MURLExerror: " + ex.getMessage(), ex);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        Log.e("Debug", "IOEx error: " + ioe.getMessage(), ioe);
        uploadTask.cancel(true);
        return null;
    } finally {
        try {
            is.close();
            dos.flush();
            dos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    // ------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream(conn.getInputStream());

        if ((jsonResponse = inStream.readLine()) != null) {
            Log.e("Debug", "Server Response " + jsonResponse);

        } else {
            jsonResponse = "";
        }

        inStream.close();
        conn.disconnect();
        //
    } catch (IOException ioex) {
        ioex.printStackTrace();
        Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    }

    return jsonResponse;
}
Run Code Online (Sandbox Code Playgroud)

323*_*3go 5

如果接收服务器支持它,您可以使用Content-Range标头来标识恢复上载.Google-Drive API支持它.如果您自己动手,我会遵循Google使用的模式:

  • 开始上传并获取会话标识符.
  • 上传中断后,请等待互联网恢复.
  • 恢复上传时,首先询问服务器收到的字节数.(*)
  • 使用服务器状态后的下一个字节继续上传.

(*)请注意,当我之前自己滚动时,我添加了来自服务器的编码响应,其中包含上传的最后一个KB,只是为了验证它在传输中没有损坏.但是,我从未在生产中看到服务器收到损坏数据的情况.