【问题标题】:Java - Getting corrupted JPG while uploading using FTP connection?Java - 使用 FTP 连接上传时损坏的 JPG?
【发布时间】:2013-10-21 19:35:18
【问题描述】:

以下代码用于在 JAVA 中使用 FTP 上传三个 JPG-Image 文件。 文件已上传并通过“成功”消息确认,但文件已损坏。小的缩略图文件是部分可读的(上四分之一)。

我尝试搜索网络并添加

setFileType(FTPClient.BINARY_FILE_TYPE);

但是问题还是出现了:(

有人可以看看它并给我一个提示或建议吗?

代码:

package de.immozukunft.programs;

import java.io.*;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.commons.net.ftp.FTPClient;

/**
 * This class enables the ability to connect and trasfer data to the FTP server
 */

public class FtpUpDown {

    static Locale locale = new Locale("de"); // Locale is set to "de" for
                                                // Germany
    static ResourceBundle r = ResourceBundle.getBundle("Strings", locale); // ResourceBundle
                                                                            // for
                                                                            // different
                                                                            // languages
                                                                            // and
                                                                            // String
                                                                            // Management

    // FTP-Connection properties
    static String host = "IP-address"; //Host
    static String username = "username"; // Username
    static int port = 21; //Port
    static String password = "password"; // Password

    /**
     * <h3>FTP-connection tester</h3>
     * 
     */

    public static boolean connect() {

        FTPClient ftpClient = new FTPClient();

        try {
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.logout();
            ftpClient.disconnect();
        } catch (Exception e) {
            // e.printStackTrace();
            System.err.println("Unable to connect"); // TODO String einfügen
            return (false);
        }
        System.out.println("Connection established"); // TODO String einfügen
        return (true);
    }

    /**
     * <h3>FTP-Status</h3>
     * 
     * @return
     * @throws IOException
     */
    static public String getStatus() {
        if (connect()) {
            return (r.getString("successConnectFTP"));
        } else {
            return (r.getString("unableToConnectFTP"));
        }
    }

    /**
     * <h3>FTP-filelist</h3>
     * 
     * @return String-Array der Dateinamen auf dem FTP-Server
     */

    public static String[] list() throws IOException {
        FTPClient ftpClient = new FTPClient();
        String[] filenameList;

        try {
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            filenameList = ftpClient.listNames();
            ftpClient.logout();
        } finally {
            ftpClient.disconnect();
        }

        return filenameList;
    }

    /**
     * <h3>FTP-Client-Download:</h3>
     * 
     * @return true falls ok
     */
    public static boolean download(String localResultFile,
            String remoteSourceFile, boolean showMessages) throws IOException {
        FTPClient ftpClient = new FTPClient();
        FileOutputStream fos = null;
        boolean resultOk = true;

        try {
            ftpClient.connect(host, port);
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
            resultOk &= ftpClient.login(username, password);
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
            fos = new FileOutputStream(localResultFile);
            resultOk &= ftpClient.retrieveFile(remoteSourceFile, fos);
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
            resultOk &= ftpClient.logout();
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {/* nothing to do */
            }
            ftpClient.disconnect();
        }

        return resultOk;
    }

    /**
     * <h3>FTP-Client-Upload:</h3>
     * 
     * @param localSourceFile
     *            The source of local file
     * @param remoteResultFile
     *            Set the destination of the file
     * @param showMessages
     *            If set on TRUE messages will be displayed on the console
     * @return true Returns If successfully transfered it will return TRUE, else
     *         FALSE
     */
    public static boolean upload(String localSourceFile,
            String remoteResultFile, boolean showMessages) throws IOException {

        FTPClient ftpClient = new FTPClient();
        FileInputStream fis = null;
        boolean resultOk = true;

        try {
            ftpClient.connect(host, port);
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
            resultOk &= ftpClient.login(username, password);
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
            fis = new FileInputStream(localSourceFile);
            resultOk &= ftpClient.storeFile(remoteResultFile, fis);
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
            resultOk &= ftpClient.logout();
            if (showMessages) {
                System.out.println(ftpClient.getReplyString());
            }
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {/* nothing to do */
            }
            ftpClient.disconnect();
        }

        return resultOk;
    }

    // Setter and Getter-methods
    public static String getHost() {
        return host;
    }

    public static void setHost(String host) {
        FtpUpDown.host = host;
    }

    public static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        FtpUpDown.username = username;
    }

    public static int getPort() {
        return port;
    }

    public static void setPort(int port) {
        FtpUpDown.port = port;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        FtpUpDown.password = password;
    }
}

问候

THE-E

【问题讨论】:

    标签: java file-upload ftp jpeg corrupt


    【解决方案1】:

    所以我终于弄清楚了问题所在。

    问题是由setFileType(FTPClient.BINARY_FILE_TYPE) 的顺序引起的。它需要定位在upload()-方法中fis = new FileInputStream(localSourceFile)之后和ftpClient.storeFile(remoteResultFile, fis)之前

    所以完整的工作代码是:

    import java.io.*;
    import java.util.Locale;
    import java.util.ResourceBundle;
    
    import org.apache.commons.net.ftp.FTPClient;
    
    /**
     * This class enables the ability to connect and trasfer data to the FTP server
     */
    
    public class FtpUpDown {
    
        static Locale locale = new Locale("de"); // Locale is set to "de" for
                                                    // Germany
        static ResourceBundle r = ResourceBundle.getBundle("Strings", locale); // ResourceBundle
                                                                                // for
                                                                                // different
                                                                                // languages
                                                                                // and
                                                                                // String
                                                                                // Management
    
        // FTP-Connection properties
        static String host = "IP-Address"; // IP-address
        static String username = "username"; // Username
        static int port = 21; // Port
        static String password = "password"; // Password
    
        /**
         * <h3>FTP-connection tester</h3>
         * 
         */
    
        public static boolean connect() {
    
            FTPClient ftpClient = new FTPClient();
    
            try {
                ftpClient.connect(host, port);
                ftpClient.login(username, password);
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (Exception e) {
                // e.printStackTrace();
                System.err.println("Unable to connect"); // TODO String einfügen
                return (false);
            }
            System.out.println("Connection established"); // TODO String einfügen
            return (true);
        }
    
        /**
         * <h3>FTP-Status</h3>
         * 
         * @return
         * @throws IOException
         */
        static public String getStatus() {
            if (connect()) {
                return (r.getString("successConnectFTP"));
            } else {
                return (r.getString("unableToConnectFTP"));
            }
        }
    
        /**
         * <h3>FTP-filelist</h3>
         * 
         * @return String-Array der Dateinamen auf dem FTP-Server
         */
    
        public static String[] list() throws IOException {
            FTPClient ftpClient = new FTPClient();
            String[] filenameList;
    
            try {
                ftpClient.connect(host, port);
                ftpClient.login(username, password);
                filenameList = ftpClient.listNames();
                ftpClient.logout();
            } finally {
                ftpClient.disconnect();
            }
    
            return filenameList;
        }
    
        /**
         * <h3>FTP-Client-Download:</h3>
         * 
         * @return true falls ok
         */
    
        public static boolean download(String localResultFile,
                String remoteSourceFile, boolean showMessages) throws IOException {
            FTPClient ftpClient = new FTPClient();
            FileOutputStream fos = null;
            boolean resultOk = true;
    
            try {
                ftpClient.connect(host, port);
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
                resultOk &= ftpClient.login(username, password);
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
                fos = new FileOutputStream(localResultFile);
                resultOk &= ftpClient.retrieveFile(remoteSourceFile, fos);
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
                resultOk &= ftpClient.logout();
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {/* nothing to do */
                }
                ftpClient.disconnect();
            }
    
            return resultOk;
        }
    
    
        /**
         * <h3>FTP-Client-Upload:</h3>
         * 
         * @param localSourceFile
         *            The source of local file
         * @param remoteResultFile
         *            Set the destination of the file
         * @param showMessages
         *            If set on TRUE messages will be displayed on the console
         * @return true Returns If successfully transfered it will return TRUE, else
         *         FALSE
         */
        public static boolean upload(String localSourceFile,
                String remoteResultFile, boolean showMessages) throws IOException {
    
            FTPClient ftpClient = new FTPClient();
            FileInputStream fis = null;
            boolean resultOk = true;
    
            try {
                ftpClient.connect(host, port);
    
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
                resultOk &= ftpClient.login(username, password);
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
    
                fis = new FileInputStream(localSourceFile);
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
    
                resultOk &= ftpClient.storeFile(remoteResultFile, fis);
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
                resultOk &= ftpClient.logout();
                if (showMessages) {
                    System.out.println(ftpClient.getReplyString());
                }
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    ftpClient.disconnect();
                } catch (IOException e) {/* nothing to do */
                }
            }
    
            return resultOk;
        }   
    
    
        // Setter and Getter-methods
        public static String getHost() {
            return host;
        }
    
        public static void setHost(String host) {
            FtpUpDown.host = host;
        }
    
        public static String getUsername() {
            return username;
        }
    
        public static void setUsername(String username) {
            FtpUpDown.username = username;
        }
    
        public static int getPort() {
            return port;
        }
    
        public static void setPort(int port) {
            FtpUpDown.port = port;
        }
    
        public static String getPassword() {
            return password;
        }
    
        public static void setPassword(String password) {
            FtpUpDown.password = password;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您为每个操作创建一个新连接,并且某些代码路径没有设置“二进制文件”标志(即上传和下载方法)。

      【讨论】:

        【解决方案3】:

        setFileType(FTPClient.BINARY_FILE_TYPE) 是正确的做法,但在您提供的代码中,它只在 connect() 中完成,而在 download() 中没有调用。

        即使在download() 中调用它,也会调用disconnect() 来结束会话。

        底线:你需要在download()方法和ftpClient.connect(host, port)之后的upload()方法中调用setFileType(FTPClient.BINARY_FILE_TYPE)

        【讨论】:

        • 我试过在upload()-method和download()-method的ftpClient.connect(host,port)方法后面加上setFileType(FTPClient.BINARY_FILE_TYPE),但是结果是一样的:/
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-09
        相关资源
        最近更新 更多