当前位置: 首页 > 面试题库 >

FTP zip上传有时已损坏

邓季
2023-03-14
问题内容

我编写了一个代码,用于在文件中保存少量图像,然后压缩该文件并上传到ftp服务器。当我从服务器下载该文件时,很少有文件可用,并且很少有文件损坏。可能是什么原因呢?压缩代码或上载程序代码是否有故障。

压缩代码:

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);返回假?

顺便说一句,如果您以ASCII模式传输zip,几乎肯定会导致损坏。



 类似资料:
  • 在我的应用程序中,我需要将图像直接上传到AWS S3。为此,我的服务器生成了一个预签名的url,移动客户端使用该url来放置文件。虽然在上传调用中获得了200,但文件没有正确上传,即文件已损坏,并且从未加载回。下面是用于将文件上载到S3的代码。 我正在使用改造1.8。

  • 我只是上传文件到谷歌云存储使用签名网址与Ajax上传。 已经完全上传了。但尝试从Bucket下载文件时,文件会像媒体文件一样损坏,否则pdf、txt文件工作正常。有什么建议请建议我谢谢。

  • 问题内容: 我正在使用角度$ http从服务器下载文件。文件类型可以不同。我应该设置请求标头以进行身份​​验证。下载完成后,文件已损坏!这是我在客户端保存文件的代码: 问题答案: 我最终通过将以下配置添加到ajax请求中解决了该问题: 并将Blob类型更改为 “应用程序/八位字节流”

  • 我计划通过存储某种可以在JS中生成的校验和来实现一个解决方案,以确保数据完全存在。如果没有,则清除它并从服务器中取出。

  • 我正在使用apache poi,我创建了一个HSSF工作簿,并尝试打开一个xlsx文件。但当我用excel打开时,它显示文件已损坏。这是我的密码。

  • 我最近切换到LinuxMint,需要安装Netbean 8.2版。它安装没有问题,但每当我试图打开一个项目,它被标记为坏了。我做了一些搜索,它似乎不能识别我安装的JDK(jdk 1.8。0_131)。我试图自己做一些故障排除,但我找不到解决办法。我希望这里有人能带领我走向正确的方向。如果您需要更多信息,请告诉我。 提前谢谢!