THE*_*E-E 7 java ftp jpeg file-upload corrupt
以下代码用于在 JAVA 中使用 FTP 上传三个 JPG 图像文件。\n文件已上传并通过“成功”消息进行确认,但文件已损坏。小缩略图文件部分可读(上四分之一)。
\n\n我尝试搜索网络并添加
\n\nsetFileType(FTPClient.BINARY_FILE_TYPE);\n
Run Code Online (Sandbox Code Playgroud)\n\n但问题仍然出现:(
\n\n有人可以看一下并给我提示或建议吗?
\n\n代码:
\n\npackage de.immozukunft.programs;\n\nimport java.io.*;\nimport java.util.Locale;\nimport java.util.ResourceBundle;\n\nimport org.apache.commons.net.ftp.FTPClient;\n\n/**\n * This class enables the ability to connect and trasfer data to the FTP server\n */\n\npublic class FtpUpDown {\n\n static Locale locale = new Locale("de"); // Locale is set to "de" for\n // Germany\n static ResourceBundle r = ResourceBundle.getBundle("Strings", locale); // ResourceBundle\n // for\n // different\n // languages\n // and\n // String\n // Management\n\n // FTP-Connection properties\n static String host = "IP-address"; //Host\n static String username = "username"; // Username\n static int port = 21; //Port\n static String password = "password"; // Password\n\n /**\n * <h3>FTP-connection tester</h3>\n * \n */\n\n public static boolean connect() {\n\n FTPClient ftpClient = new FTPClient();\n\n try {\n ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);\n ftpClient.connect(host, port);\n ftpClient.login(username, password);\n ftpClient.logout();\n ftpClient.disconnect();\n } catch (Exception e) {\n // e.printStackTrace();\n System.err.println("Unable to connect"); // TODO String einf\xc3\xbcgen\n return (false);\n }\n System.out.println("Connection established"); // TODO String einf\xc3\xbcgen\n return (true);\n }\n\n /**\n * <h3>FTP-Status</h3>\n * \n * @return\n * @throws IOException\n */\n static public String getStatus() {\n if (connect()) {\n return (r.getString("successConnectFTP"));\n } else {\n return (r.getString("unableToConnectFTP"));\n }\n }\n\n /**\n * <h3>FTP-filelist</h3>\n * \n * @return String-Array der Dateinamen auf dem FTP-Server\n */\n\n public static String[] list() throws IOException {\n FTPClient ftpClient = new FTPClient();\n String[] filenameList;\n\n try {\n ftpClient.connect(host, port);\n ftpClient.login(username, password);\n filenameList = ftpClient.listNames();\n ftpClient.logout();\n } finally {\n ftpClient.disconnect();\n }\n\n return filenameList;\n }\n\n /**\n * <h3>FTP-Client-Download:</h3>\n * \n * @return true falls ok\n */\n public static boolean download(String localResultFile,\n String remoteSourceFile, boolean showMessages) throws IOException {\n FTPClient ftpClient = new FTPClient();\n FileOutputStream fos = null;\n boolean resultOk = true;\n\n try {\n ftpClient.connect(host, port);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.login(username, password);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n fos = new FileOutputStream(localResultFile);\n resultOk &= ftpClient.retrieveFile(remoteSourceFile, fos);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.logout();\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n } finally {\n try {\n if (fos != null) {\n fos.close();\n }\n } catch (IOException e) {/* nothing to do */\n }\n ftpClient.disconnect();\n }\n\n return resultOk;\n }\n\n /**\n * <h3>FTP-Client-Upload:</h3>\n * \n * @param localSourceFile\n * The source of local file\n * @param remoteResultFile\n * Set the destination of the file\n * @param showMessages\n * If set on TRUE messages will be displayed on the console\n * @return true Returns If successfully transfered it will return TRUE, else\n * FALSE\n */\n public static boolean upload(String localSourceFile,\n String remoteResultFile, boolean showMessages) throws IOException {\n\n FTPClient ftpClient = new FTPClient();\n FileInputStream fis = null;\n boolean resultOk = true;\n\n try {\n ftpClient.connect(host, port);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.login(username, password);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n fis = new FileInputStream(localSourceFile);\n resultOk &= ftpClient.storeFile(remoteResultFile, fis);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.logout();\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n } finally {\n try {\n if (fis != null) {\n fis.close();\n }\n } catch (IOException e) {/* nothing to do */\n }\n ftpClient.disconnect();\n }\n\n return resultOk;\n }\n\n // Setter and Getter-methods\n public static String getHost() {\n return host;\n }\n\n public static void setHost(String host) {\n FtpUpDown.host = host;\n }\n\n public static String getUsername() {\n return username;\n }\n\n public static void setUsername(String username) {\n FtpUpDown.username = username;\n }\n\n public static int getPort() {\n return port;\n }\n\n public static void setPort(int port) {\n FtpUpDown.port = port;\n }\n\n public static String getPassword() {\n return password;\n }\n\n public static void setPassword(String password) {\n FtpUpDown.password = password;\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n问候
\n\nTHE-E
\n所以我终于弄清楚问题出在哪里了。
\n\n该问题是由 的顺序引起的setFileType(FTPClient.BINARY_FILE_TYPE)
。它需要放置在upload()
-method 的后面fis = new FileInputStream(localSourceFile)
和之前ftpClient.storeFile(remoteResultFile, fis)
所以完整的工作代码是:
\n\nimport java.io.*;\nimport java.util.Locale;\nimport java.util.ResourceBundle;\n\nimport org.apache.commons.net.ftp.FTPClient;\n\n/**\n * This class enables the ability to connect and trasfer data to the FTP server\n */\n\npublic class FtpUpDown {\n\n static Locale locale = new Locale("de"); // Locale is set to "de" for\n // Germany\n static ResourceBundle r = ResourceBundle.getBundle("Strings", locale); // ResourceBundle\n // for\n // different\n // languages\n // and\n // String\n // Management\n\n // FTP-Connection properties\n static String host = "IP-Address"; // IP-address\n static String username = "username"; // Username\n static int port = 21; // Port\n static String password = "password"; // Password\n\n /**\n * <h3>FTP-connection tester</h3>\n * \n */\n\n public static boolean connect() {\n\n FTPClient ftpClient = new FTPClient();\n\n try {\n ftpClient.connect(host, port);\n ftpClient.login(username, password);\n ftpClient.logout();\n ftpClient.disconnect();\n } catch (Exception e) {\n // e.printStackTrace();\n System.err.println("Unable to connect"); // TODO String einf\xc3\xbcgen\n return (false);\n }\n System.out.println("Connection established"); // TODO String einf\xc3\xbcgen\n return (true);\n }\n\n /**\n * <h3>FTP-Status</h3>\n * \n * @return\n * @throws IOException\n */\n static public String getStatus() {\n if (connect()) {\n return (r.getString("successConnectFTP"));\n } else {\n return (r.getString("unableToConnectFTP"));\n }\n }\n\n /**\n * <h3>FTP-filelist</h3>\n * \n * @return String-Array der Dateinamen auf dem FTP-Server\n */\n\n public static String[] list() throws IOException {\n FTPClient ftpClient = new FTPClient();\n String[] filenameList;\n\n try {\n ftpClient.connect(host, port);\n ftpClient.login(username, password);\n filenameList = ftpClient.listNames();\n ftpClient.logout();\n } finally {\n ftpClient.disconnect();\n }\n\n return filenameList;\n }\n\n /**\n * <h3>FTP-Client-Download:</h3>\n * \n * @return true falls ok\n */\n\n public static boolean download(String localResultFile,\n String remoteSourceFile, boolean showMessages) throws IOException {\n FTPClient ftpClient = new FTPClient();\n FileOutputStream fos = null;\n boolean resultOk = true;\n\n try {\n ftpClient.connect(host, port);\n ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.login(username, password);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n fos = new FileOutputStream(localResultFile);\n resultOk &= ftpClient.retrieveFile(remoteSourceFile, fos);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.logout();\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n } finally {\n try {\n if (fos != null) {\n fos.close();\n }\n } catch (IOException e) {/* nothing to do */\n }\n ftpClient.disconnect();\n }\n\n return resultOk;\n }\n\n\n /**\n * <h3>FTP-Client-Upload:</h3>\n * \n * @param localSourceFile\n * The source of local file\n * @param remoteResultFile\n * Set the destination of the file\n * @param showMessages\n * If set on TRUE messages will be displayed on the console\n * @return true Returns If successfully transfered it will return TRUE, else\n * FALSE\n */\n public static boolean upload(String localSourceFile,\n String remoteResultFile, boolean showMessages) throws IOException {\n\n FTPClient ftpClient = new FTPClient();\n FileInputStream fis = null;\n boolean resultOk = true;\n\n try {\n ftpClient.connect(host, port);\n\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.login(username, password);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n\n fis = new FileInputStream(localSourceFile);\n ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);\n\n resultOk &= ftpClient.storeFile(remoteResultFile, fis);\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n resultOk &= ftpClient.logout();\n if (showMessages) {\n System.out.println(ftpClient.getReplyString());\n }\n } finally {\n try {\n if (fis != null) {\n fis.close();\n }\n ftpClient.disconnect();\n } catch (IOException e) {/* nothing to do */\n }\n }\n\n return resultOk;\n } \n\n\n // Setter and Getter-methods\n public static String getHost() {\n return host;\n }\n\n public static void setHost(String host) {\n FtpUpDown.host = host;\n }\n\n public static String getUsername() {\n return username;\n }\n\n public static void setUsername(String username) {\n FtpUpDown.username = username;\n }\n\n public static int getPort() {\n return port;\n }\n\n public static void setPort(int port) {\n FtpUpDown.port = port;\n }\n\n public static String getPassword() {\n return password;\n }\n\n public static void setPassword(String password) {\n FtpUpDown.password = password;\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
2213 次 |
最近记录: |