久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

FTP zip 上傳有時會損壞

FTP zip upload is corrupted sometimes(FTP zip 上傳有時會損壞)
本文介紹了FTP zip 上傳有時會損壞的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我編寫了一個代碼,用于將少量圖像保存在一個文件中,然后壓縮該文件并上傳到 ftp 服務器.當我從服務器下載它時,很少有文件很好,也很少有文件損壞.這可能是什么原因?壓縮代碼或上傳代碼是否有問題.

I wrote a code for saving few images in a file and later compressing that file and uploading to an ftp server. When I download that from server, few files are fine and few files got corrupted. What can be the reason for that? Whether there may be a fault with Compress code or uploader code.

壓縮代碼:

public class Compress {

private static final int BUFFER = 2048;

private ArrayList<String> _files;
private String _zipFile;

public Compress(ArrayList<String> files, String zipFile) {
    Log.d("Compress", "Compressing started");
    _files = files;
    _zipFile = zipFile;
}

public void zip() {
    try {
        BufferedInputStream origin = null;
        File f = new File(_zipFile);
        if (f.exists())
            f.delete();
        FileOutputStream dest = new FileOutputStream(_zipFile);

        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                dest));

        byte data[] = new byte[BUFFER];

        for (int i = 0; i < _files.size(); i++) {
            Log.v("Compress", "Adding: " + _files.get(i));
            FileInputStream fi = new FileInputStream(_files.get(i));
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(_files.get(i).substring(
                    _files.get(i).lastIndexOf("/") + 1));
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }

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

}

}

FTP上傳代碼:

public class UploadZipFiles extends AsyncTask<Object, Integer, Object> {
    ArrayList<String> zipFiles;
    String userName, password;
    WeakReference<ServiceStatusListener> listenerReference;
    private long totalFileSize = 0;
    protected long totalTransferedBytes = 0;
    final NumberFormat nf = NumberFormat.getInstance();

    public UploadZipFiles(ServiceStatusListener listener,
            ArrayList<String> zipFiles, String userName, String password) {
        Log.d("u and p", "" + userName + "=" + password);
        this.zipFiles = zipFiles;
        this.userName = userName;
        this.password = password;
        this.listenerReference = new WeakReference<ServiceStatusListener>(
                listener);
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);

    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        for (String file : zipFiles) {
            totalFileSize = totalFileSize + new File(file).length();
        }
    }

    @Override
    protected Object doInBackground(Object... arg0) {
        CustomFtpClient ftpClient = new CustomFtpClient();

        try {

            ftpClient.connect(ftpUrl);

            // change here
            ftpClient.login(userName, password);

            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            for (String file : zipFiles) {

                InputStream in;

                in = new FileInputStream(new File(file));

                ftpClient.storeFile(new File(file).getName(), in);

                in.close();
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // TODO add files to ftp

        return "Success";
    }

    @Override
    protected void onPostExecute(Object result) {
        if (result instanceof Exception) {
            listenerReference.get().onFailure(
                    new Exception(result.toString()));
        } else {
            listenerReference.get().onSuccess("Success");
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {

        UploadActivity.progressBar
                .setProgress((int) (((float) values[0] / totalFileSize) * 100));
        UploadActivity.uploadingSizeTextView.setText(nf
                .format(((float) values[0] / (1024 * 1024)))
                + " mb of "
                + nf.format(((float) totalFileSize / (1024 * 1024)))
                + " mb uploaded");
    }

    public class CustomFtpClient extends FTPClient {

        public boolean storeFile(String remote, InputStream local)
                throws IOException {
            OutputStream output;
            Socket socket;

            if ((socket = _openDataConnection_(FTPCommand.STOR, remote)) == null)
                return false;

            output = new BufferedOutputStream(socket.getOutputStream(),
                    getBufferSize());
            // if (__fileType == ASCII_FILE_TYPE)
            // output = new ToNetASCIIOutputStream(output);
            // Treat everything else as binary for now
            try {
                Util.copyStream(local, output, getBufferSize(),
                        CopyStreamEvent.UNKNOWN_STREAM_SIZE,
                        new CopyStreamListener() {
                            @Override
                            public void bytesTransferred(
                                    long totalBytesTransferred,
                                    int bytesTransferred, long streamSize) {
                                totalTransferedBytes = totalTransferedBytes
                                        + bytesTransferred;
                                publishProgress((int) totalTransferedBytes);
                                // UploadActivity.uploadingSizeTextView.setText(nf
                                // .format((totalBytesTransferred / (1024 *
                                // 1024)))
                                // + " mb of "
                                // + nf.format((totalFileSize / (1024 *
                                // 1024)))
                                // + " mb uploaded");
                            }

                            @Override
                            public void bytesTransferred(
                                    CopyStreamEvent arg0) {
                                // TODO Auto-generated method stub

                            }
                        });
                // Util.copyStream(local, output, getBufferSize(),
                // CopyStreamEvent.UNKNOWN_STREAM_SIZE, null, false);

            } catch (IOException e) {
                try {
                    socket.close();
                } catch (IOException f) {
                }
                throw e;
            }
            output.close();
            socket.close();
            return completePendingCommand();
        }
    }
}

推薦答案

一定要傳輸BINARY_FILE_TYPE中的文件.也許 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 返回 false?

Be sure to transfer files in BINARY_FILE_TYPE. Maybe ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); returns false?

順便說一句,如果您以 ASCII 模式傳輸 zip,這幾乎肯定會導致損壞.

By the way, if you transfer a zip in ASCII-mode, this will almost surely result corrupted.

這篇關于FTP zip 上傳有時會損壞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 免费在线观看黄视频 | 久久久久99| zzzwww在线看片免费 | 欧美日本在线 | 日韩视频中文字幕 | 久久国产精品一区二区 | 国产精品一区二区三区久久久 | a视频在线观看 | 日韩欧美在线不卡 | 青青草国产在线观看 | 亚洲视频在线看 | 91精品国产91久久久久久密臀 | 成人在线视频网 | 91色在线视频 | 国产精品免费看 | 狠狠干五月天 | 国产一区二区av | 久久人人网 | 亚洲欧美日韩在线 | 亚洲国产一区二区三区在线观看 | 日韩欧美不卡 | 国产高清久久久 | 成人精品国产 | 婷婷色国产偷v国产偷v小说 | 亚洲美乳中文字幕 | 中文字幕乱码视频32 | 美女一区| 中文字幕免费在线 | 精品无码久久久久久国产 | 精品一区二区三区在线观看 | 日本三级网 | 成人国产精品色哟哟 | 国产一区二区免费 | 日本精品免费 | 91精品免费| 国产精品久久二区 | a天堂在线 | 伊人网综合在线观看 | 美女视频一区二区 | 亚洲精品久久久久久国产精华液 | 日韩图区 |