FTP客户端 - Android图像已损坏

时间:2014-04-23 07:58:27

标签: android apache ftp ftp-client

我正在尝试在我的Android应用上实施FTP Java库,以将图像上传到FTP服务器

该库是apache-commons-net的3.4版本。

问题是上传图片时,文件已损坏且无法显示。

这是代码的一部分。

首先连接到服务器并进行配置,(工作正常)

public boolean ftpConnect(String host, String username,
                          String password, int port)
{
    try {
        mFTPClient = new FTPClient();
        mFTPClient.connect(host, port);

        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {

            boolean status = mFTPClient.login(username, password);
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();
            return status;
        }
    } catch(Exception e) {
       e.printStackTrace();
        Log.d(TAG, "Error: could not connect to host " + host );
    }

    return false;
} 

然后上传文件并显示进度

public boolean ftpUpload() {

boolean finalizado = false;
String destDir="";

try {
    mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
    mFTPClient.enterLocalPassiveMode();


    while(mFilesUploaded < f.size() && !cancel){

    tmpFile = f.get(mCurrentFileIndex);

    String[] fileAux2 = tmpFile.getName().split("\\(");
    String[] parentAux = tmpFile.getParent().split("\\/");
    System.out.println(tmpFile.getName());


    if(isBackup){

        if(parentAux[parentAux.length - 1].equals("databases")) {destDir ="Backup/db";}
        else { destDir ="Backup/data/" + parentAux[parentAux.length - 1];}      

    }
    else{ destDir = "/"+fileAux2[0]; }

    System.out.println(destDir + " -> " + checkDirectoryExists(destDir));

    if(!checkDirectoryExists(destDir)) ftpCreateDirectoryTree(destDir);
    else ftpChangeDirectory(destDir);

    String filePath = destDir + "/" + tmpFile.getName();



    //if(checkFileExists(filePath)) {System.out.println("intento eliminar " + ftpRemoveFile(filePath));}

    InputStream srcFileStream = new FileInputStream(tmpFile);
    OutputStream out = mFTPClient.storeFileStream(tmpFile.getName());

    byte[] buffer = new byte[1024];
    int read;
    int allRead = 0;

    while( ( ( read = srcFileStream.read( buffer ) ) != - 1 )  && !cancel) {
        allRead += read;                
        Double porcentaje =  ((double) allRead / (double)tmpFile.length()) * 100;
        publishProgress((int) Math.round(porcentaje));              
        out.write( buffer, 0, read );

    }

    srcFileStream.close();
    out.flush();
    out.close();
    if(read == -1 && mFTPClient.completePendingCommand() && !cancel){
        publishProgress(100);
        mFilesUploaded++;
        mCurrentFileIndex++;

        }
    else{
        publishProgress(-1);

    }

    }
            if(cancel == true){cancel = false; finalizado = false;}
            else finalizado = true;

            mCurrentFileIndex = 0;
            mFilesUploaded = 0;                 
            ftpDisconnect(); 


} 
catch (Exception e) {

    e.printStackTrace();
}

return finalizado;

}

编辑:上传文件时,其尺寸小于原始尺寸。(mFTPClient.setFileType(FTP.BINARY_FILE_TYPE)已配置。)

如果我减小byte[] buffer = new byte[1024];的字节大小,文件大小会增加,但上传时间太慢

有人可以帮助我吗?

0 个答案:

没有答案
相关问题