簡體   English   中英

Java - 使用FTP連接上傳時損壞的JPG?

[英]Java - Getting corrupted JPG while uploading using FTP connection?

以下代碼用於在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

所以我終於弄明白了問題所在。

問題是由setFileType(FTPClient.BINARY_FILE_TYPE)的順序引起的。 它需要在fis = new FileInputStream(localSourceFile)ftpClient.storeFile(remoteResultFile, fis) fis = new FileInputStream(localSourceFile)之前定位在upload() - 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;
    }
}

您為每個操作創建一個新連接,並且某些代碼路徑不設置“二進制文件”標志(即上載和下載方法)。

setFileType(FTPClient.BINARY_FILE_TYPE)是正確的做法,但在你提供的代碼中,它只在connect() ,而在download()沒有調用。

即使在download() disconnect()中調用它,也會調用disconnect()來結束會話。

結論:你需要在download()方法中調用setFileType(FTPClient.BINARY_FILE_TYPE)setFileType(FTPClient.BINARY_FILE_TYPE) ftpClient.connect(host, port)之后調用upload()方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM