如何在Android中恢复文件上传过程? [英] How to Resume file uploading process in Android?

查看:78
本文介绍了如何在Android中恢复文件上传过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码在服务器上上传文件,但是我想要这样的功能:如果由于网络丢失或过程中的任何其他中断而停止,则不应从第二次开始上传.来自服务器的响应也是可定制的.有可能在android中吗?我应该使用什么方法来做到这一点?请指导我. 如果可能的话,请给我建议任何示例代码.

I am uploading a file on server using this code, But I want such a functionality: if it stops due to lost a network or any other interruption during the process, then it shouldn't be start uploading from beginning at second time. response from server is also customizable. is it possible in android? what approach should I use to do this? please guide me. and if possible please suggest me any sample piece of code.

谢谢!

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;
}

推荐答案

如果接收服务器支持,则可以使用Content-Range标头标识继续上传. Google-Drive API支持该功能.如果您自己动手,我会遵循Google使用的模式:

If the receiving server supports it, you can use the Content-Range header to identify a resuming upload. The Google-Drive API supports it. If you roll your own, I'd follow the pattern Google uses:

  • 开始上传并获取会话标识符.
  • 当上传中断时,请等到互联网恢复正常.
  • 恢复上载时,首先查询服务器已收到多少字节. (*)
  • 使用服务器状态后的下一个字节恢复上传.

(*)请注意,当我之前滚动自己的内容时,我添加了来自服务器的编码后响应,带有最后一个上传的KB,目的是验证它在传输中没有损坏.但是,在生产环境中,我从未见过服务器接收到损坏的数据的情况.

(*) Note that when I rolled my own before, I added an encoded response from the server with the last KB of upload, just to verify it wasn't corrupted in transfer. However, I've never, in production, seen a case where a server had received corrupt data.

这篇关于如何在Android中恢复文件上传过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆