无法使用Apache FTPClient将文件上传到Android应用程序中的FTPServer

时间:2016-03-04 07:03:15

标签: android ftp ftp-client ftp-server

我试图从Android应用程序上传大小为2MB的ZIP文件到FTPServer。验证后,如果不存在,我在服务器中创建必要的文件夹,稍后我尝试上传zip文件,但最终失败并出现以下错误。

ftpUpload: libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:588)
libcore.io.IoBridge.recvfrom(IoBridge.java:552)
java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
java.net.PlainSocketImpl.-wrap0(PlainSocketImpl.java)
java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
java.io.InputStreamReader.read(InputStreamReader.java:233)
java.io.BufferedReader.fillBuf(BufferedReader.java:145)
java.io.BufferedReader.readLine(BufferedReader.java:397)
org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:490)
org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:534)
org.apache.commons.net.ftp.FTP.port(FTP.java:862)
org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:463)
org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:374)
org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1379)
com.inspection.servertransaction.CaseUploader.ftpUpload(CaseUploader.java:775)
com.inspection.servertransaction.CaseUploader.zipMedia(CaseUploader.java:591)
com.inspection.servertransaction.CaseUploader.uploadCase(CaseUploader.java:194)
com.inspection.services.UploadCasesThread$WorkerThread.processCommand(UploadCasesThread.java:103)
com.inspection.services.UploadCasesThread$WorkerThread.run(UploadCasesThread.java:51)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
java.lang.Thread.run(Thread.java:818)

FTPUpload 功能

private boolean ftpUpload(File localFilePath, String remoteFilePath) {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis;
    boolean result;
    try {
        ftpclient.connect(InetAddress.getByName(context.getString(R.string.ftp_hostname)));
        boolean isLoggedIn = ftpclient.login(context.getString(R.string.ftp_username), context.getString(R.string.ftp_password));

        if (isLoggedIn) {
            Log.i(this.getClass().getSimpleName(), "FTP authenticated");
            // On Successful login
            ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

            // Ignore the actual file name from remote path, and get only directory path.
            int index = remoteFilePath.lastIndexOf("/");
            String remoteDir = null;
            if (index != -1)
                remoteDir = remoteFilePath.substring(0, index);

            if (remoteDir != null)
                /**
                 * changeWorkingDirectory returns whether the specified directory exist or not.
                 * If not exist in remote, create folders
                 */
                if (!ftpclient.changeWorkingDirectory(remoteDir)) {
                    if (makeDirectories(ftpclient, remoteDir))
                        ftpclient.changeWorkingDirectory(remoteFilePath);
                    else
                        Log.e(this.getClass().getSimpleName(), "remote path is not available");
                } else
                    ftpclient.changeWorkingDirectory(remoteFilePath);

            // Get the stream of the file
            String testName = localFilePath.getName();
            fis = new FileInputStream(localFilePath);

            // Upload file to the ftp server
            result = ftpclient.storeFile(testName, fis);
            Log.i(this.getClass().getSimpleName(), "FTP Store Result " + result);
            if (result) {
                Log.i(this.getClass().getSimpleName(), "File is uploaded successfully");
                result = true;
            } else {
                Log.e(this.getClass().getSimpleName(), "File uploading failed");
                result = false;
            }
        } else {
            Log.e(this.getClass().getSimpleName(), "Login Fail!");
            result = false;
        }

        // logout from ftp server
        ftpclient.logout();

    } catch (Exception e) {
        Log.e(this.getClass().getSimpleName(), "FTP Exception!");
        EvApp.mErrorLogger.writeLog(ExceptionStackTrace.printStackTraceException(e),
                this.getClass().getSimpleName() + ": ftpUpload", null, "Error");
        result = false;
    }

工作正常,突然面临这个问题。所以我怀疑它可能是服务器方面的问题。但问题可能是什么?任何帮助将不胜感激。

0 个答案:

没有答案