1

大きなファイルをチャンク単位で PHP サーバーにアップロードして、接続が切断された場合にいつでもアップロードを再開できるようにするにはどうすればよいでしょうか。

具体的には、Android でこれを行うにはどのライブラリが必要ですか?

ユーザーは、インターネット接続が低速または不安定な国から大きなファイルをアップロードしています。ありがとうございました

編集

詳細については、現在 HTTP POST を使用してファイル全体を一度にアップロードしています。次のコードが示すように:

private int uploadFiles(File file) {
        String zipName = file.getAbsolutePath() + ".zip";
        if(!zipFiles(file.listFiles(), zipName)){
            //return -1;
            publishResults(-1);
        }
        //publishProgress(-1, 100);
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String serverUrl = prefs.getString("serverUrl", "ServerGoesHere"); // todo ensure that a valid string is always stored
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        int responseCode = -1;
        try {
            //notif title, undeterministic
            pNotif.setContentText("Zipping complete. Now Uploading...")
                  .setProgress(0, 0, true);
            mNotifyManager.notify(NOTIFICATION_ID, pNotif.build()); // make undeterministic

            //update progress bar to indeterminate
            sendUpdate(0, 0, "Uploading file."); // sendupdate using intent extras

            File uploadFile = new File(zipName);
            long totalBytes = uploadFile.length();
            FileInputStream fileInputStream = new FileInputStream(uploadFile);

            URL url = new URL(serverUrl);
            connection = (HttpURLConnection) url.openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            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=\"uploadedfile\";filename=\""
                    + zipName + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            long bytesUploaded = 0;
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                bytesUploaded += bytesRead;
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                //int percentCompleted = (int) ((100 * bytesUploaded) / totalBytes);
                //publishProgress((int)bytesUploaded/1024, (int)totalBytes/1024);

                System.out.println("bytesRead> " + bytesRead);
            }

            //publishProgress(-2, 1); // switch to clean up
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                    + lineEnd);
            try {
                responseCode = connection.getResponseCode();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
            // Delete the zip file
            new File(zipName).delete();
        } catch (Exception ex) {
            new File(zipName).delete();
            responseCode = -1;
            ex.printStackTrace();
        } 
        return responseCode;
    }

これをチャンクで送信するように変更する方法はありますか? 私が行った調査のほとんどはあまり明確ではありませんでした、申し訳ありません

4

2 に答える 2

0

HTTP 経由でファイルをチャンク単位でアップロードすることはお勧めできません。HTTP はステートレス プロトコルであり、サーバーに送信するチャンクごとに新しい接続を確立する必要があるためです。さらに、この転送の間の状態を手動で維持する必要があり、ファイルが送信された順序で到着するという保証はありません。

ファイル全体が送信されるまで接続を維持する TCP ソケットでソケット プログラミングを使用する必要があります。その後、チャンクをソケットにプッシュすると、チャンクは損失なく到着し、ソケットに供給されるのと同じ順序になります。

于 2014-06-17T14:10:07.293 に答える
0

SFTP ライブラリを使用して、再開可能なアップロードを実装することになりました。JSch http://www.jcraft.com/jsch/ SFTP を使用してアップロードすると、ライブラリが再開可能モードを処理します。コード例:

JSch jsch = new JSch();
session = jsch.getSession(FTPS_USER,FTPS_HOST,FTPS_PORT);
session.setPassword(FTPS_PASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(FTPS_PATH);

File uploadFile = new File(zipName); // File to upload
totalSize = uploadFile.length(); // size of file

// If part of the file has been uploaded, it saves the number of bytes. Else 0
try {
    totalTransfer = channelSftp.lstat(uploadFile.getName()).getSize();
} catch (Exception e) {
    totalTransfer = 0;
}

// Upload File with the resumable attribute
channelSftp.put(new FileInputStream(uploadFile), uploadFile.getName(), new SystemOutProgressMonitor(), ChannelSftp.RESUME);

channelSftp.exit();
session.disconnect();

このライブラリを使用して、再開可能なアップロードとアップロードの進行状況というすべての要件を満たしました。

于 2014-08-04T18:18:05.463 に答える