开发者

Strange problem while sending file from Android to server!

开发者 https://www.devze.com 2023-02-19 08:19 出处:网络
I encountered a very strange problem while sending files, such as pictures, text and zip files to server via ftp. Most of the time, it works fine. But sometimes, the server only has part of the file.

I encountered a very strange problem while sending files, such as pictures, text and zip files to server via ftp. Most of the time, it works fine. But sometimes, the server only has part of the file. On Android, I use com.enterprisedt.net.ftp. Here is the code piece to send a file:

public void ftpUploadFiles(ArrayList<String> fileList, boolean bDeleteAfterUploaded)
{
    if(fileList.size() <= 0)
        return;

    // set up to transfer the files
    FileTransferClient ftp = null;
    try 
    {
        //Make sure there is only FTP in the whole system at any given time.
        synchronized(this)
        {
            // create client
            ftp = new FileTransferClient();
            // set remote host
            ftp.setRemoteHost("xxxxxxx");

            ftp.setUserName("xxxxxx");
            ftp.setPassword("xxxxx");

            // connect to the server
            ftp.connect();
            ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
            //1. Upload each file
            for(int i = 0 ; i < fileList.size(); i++)
            {
                if(!FileKit.fileExist(fileList.get(i)))
                    continue;

                ftp.uploadFile(fileList.get(i), FileKit.getFileName(fileList.get(i)));

                if(bDeleteAfterUploaded)
                    FileKit.fileDelete(fileList.get(i));
            }

            ftp.disconnect();
        } //End of synchronized
    } cat开发者_C百科ch (Exception e) 
    {
        FileKit.handleException(e);
    }
}

FileKit is a static wrap-up class for regular file functions. ftpUploadFiles() is called in a separate thread by an Intent so it can run in background. What seems to happen is, the ftp stops before finish transferring the file completely, so the server only gets part of the file. Based on the code above, what could possibly cause the problem? Or is it possible that is a issue with com.enterprisedt.net.ftp?

Thanks.


Your code appears to handle complete files only. So it should either succeed or fail for complete files only. That leaves only the com.enterprisedt.net.ftp package, which may send files in parts, for example when network failures occur.

There can be a problem if the transfer is interrupted due to network congestion, etc. "If the network connection is interrupted, the server may still think you are connected (as quit() has not been called). Hence a new connection and attempt to resume may fail for the reason given below." (https://enterprisedt.com/forums/viewtopic.php?t=960)

So I think that either the ftp software has a bug on network failure, or your code isn't handling the ftp software's handling of network failures.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号