久久久久久久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久久免费精品国产男女高不卡 | 亚洲欧洲成人av每日更新 | 久久精品欧美电影 | 91社区在线观看播放 | 一区二区在线免费观看 | 九九九视频在线观看 | 超碰电影 | 一区二区三区精品视频 | 久久久成人网 | 国产精品成人在线播放 | 亚洲美女av网站 | 97伦理电影网 | 丁香五月缴情综合网 | 亚洲国产精品成人久久久 | 精品视频在线免费观看 | 欧美日韩国产欧美 | 亚洲日本乱码在线观看 | 日韩欧美一区二区三区 | 亚洲va欧美va天堂v国产综合 | www,黄色,com | 精品一区二区三区中文字幕 | 欧美精品1区 | 精品一区在线免费观看 | av一区二区三区四区 | 成人免费高清 | 久久精品欧美一区二区三区不卡 | 丁香五月网久久综合 | 97免费视频在线观看 | 一区在线观看 | 久久一区二区三区四区 | 国产乱码精品一区二区三区五月婷 | 天天干天天爱天天 |