package ng.db; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import sun.misc.BASE64Encoder; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; import java.util.Random; public class toFtp { /** * »ñȡһ¸öftpÁ¬½Ó * @param host ipµØÖ· * @param port ¶Ë¿Ú * @param username Óû§Ãû * @param password ÃÜÂë * @return ·µ»ØftpÁ¬½Ó¶ÔÏó * @throws Exception Á¬½Óftpʱ·¢ÉúµÄ¸÷ÖÖÒì³£ */ public static FTPClient getFtpClient(String host, Integer port, String username, String password) throws Exception{ FTPClient ftpClient = new FTPClient(); // Á¬½Ó·þÎñÆ÷ ftpClient.connect(host, port); int reply = ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)){ ftpClient.disconnect(); return null; } // µÇÈë·þÎñÆ÷ boolean login = ftpClient.login(username, password); if(!login){ ftpClient.logout(); ftpClient.disconnect(); return null; } // Á¬½Ó²¢Çҳɹ¦µÇ½ftp·þÎñÆ÷ // ÉèÖÃͨµÀ×Ö·û¼¯£¬ ÒªÓë·þÎñ¶ËÉèÖÃÒ»Ö ftpClient.setControlEncoding("UTF-8"); // ÉèÖÃÎļþ´«Êä±àÂëÀàÐÍ£¬ ×Ö½Ú´«Ê䣺BINARY_FILE_TYPE, Îı¾´«Ê䣺ASCII_FILE_TYPE£¬ ½¨ÒéʹÓÃBINARY_FILE_TYPE½øÐÐÎļþ´«Êä ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // ¶¯Ä£Ê½: enterLocalActiveMode(),±»¶¯Ä£Ê½: enterLocalPassiveMode(),Ò»°ãÑ¡Ôñ±»¶¯Ä£Ê½ ftpClient.enterLocalPassiveMode(); // Çл»Ä¿Â¼ //ftpClient.changeWorkingDirectory("xxxx"); return ftpClient; } /** * ¶Ï¿ªftpÁ¬½Ó * @param ftpClient ftpÁ¬½Ó¿Í»§¶Ë */ public static void disConnect(FTPClient ftpClient){ if(ftpClient == null){ return; } try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } /** * ÎļþÏÂÔØ * @param ftpClient ftpÁ¬½Ó¿Í»§¶Ë * @param path Îļþ·¾¶ * @param fileName ÎļþÃû³Æ */ public static ArrayList download(FTPClient ftpClient, String path, String fileName) throws Exception { if(ftpClient == null || path == null || fileName == null){ return null; } // ÖÐÎÄĿ¼´¦Àí´æÔÚÎÊÌ⣬ ת»¯ÎªftpÄܹ»Ê¶±ðÖÐÎĵÄ×Ö·û¼¯ String remotePath; try { remotePath = new String(path.getBytes(StandardCharsets.UTF_8), FTP.DEFAULT_CONTROL_ENCODING); } catch (UnsupportedEncodingException e) { remotePath = path; } //»ñȡָ¶¨Â·¾¶ÎļþÀïÃæÍ¼Æ¬´æ·ÅµÄ·¾¶ InputStream inputStream = ftpClient.retrieveFileStream(remotePath); ArrayList getPictureList=new ArrayList(); ArrayList folderNameList=new ArrayList(); try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"utf-8"))) { String line; while ((line = br.readLine()) != null) { folderNameList.add(line); } getPictureList.add(folderNameList.get(folderNameList.size()-2)); getPictureList.add(folderNameList.get(folderNameList.size()-1)); } catch (IOException e) { e.printStackTrace(); } inputStream.close(); ftpClient.completePendingCommand(); ArrayList result=new ArrayList(); for( String item: getPictureList){ //System.out.println(item); InputStream InputStream = ftpClient.retrieveFileStream("./data/"+item); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); try{ byte[] buffer = new byte[1024*900]; int i; while ( (i = InputStream.read(buffer)) != -1) { outStream.write(buffer, 0, i); //w outputStream.flush(); } buffer = outStream.toByteArray(); String picture="data:image/jpeg;base64,"+ Base64.getEncoder().encodeToString(buffer);; result.add(picture); } catch (Exception e) { } InputStream.close(); outStream.close(); ftpClient.completePendingCommand(); } //InputStream inputStream1=ftpClient.retrieveFileStream("./data/"+lujin); // System.out.println("---"); // System.out.println(inputStream1); // if (inputStream1 == null) { // return; // } // FileOutputStream outputStream = new FileOutputStream("D:\\picture\\" + "1.jpg"); // BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream1); // BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); // try{ // byte[] buffer = new byte[2048]; // int i; // while ((i = bufferedInputStream.read(buffer)) != -1) { // bufferedOutputStream.write(buffer, 0, i); // bufferedOutputStream.flush(); // } // } catch (Exception e) { // // } // inputStream1.close(); // outputStream.close(); // bufferedInputStream.close(); // bufferedOutputStream.close(); // ¹Ø±ÕÁ÷Ö®ºó±ØÐëÖ´ÐУ¬·ñÔòÏÂÒ»¸öÎļþµ¼ÖÂÁ÷Ϊ¿Õ // boolean complete = ftpClient.completePendingCommand(); // if(complete){ // //System.out.println("Îļþ{}ÏÂÔØ³É¹¦"+ remotePath); // }else{ // //System.out.println("Îļþ{}ÏÂÔØÊ§°Ü"+ remotePath); // } return result; } /** * ÉÏ´«Îļþ * @param ftpClient ftpÁ¬½Ó¿Í»§¶Ë * @param sourcePath Ô´µØÖ· */ public static void upload(FTPClient ftpClient, String sourcePath) throws Exception{ if(ftpClient == null || sourcePath == null){ return; } File file = new File(sourcePath); if(!file.exists() || !file.isFile()){ return; } // ÖÐÎÄĿ¼´¦Àí´æÔÚÎÊÌ⣬ ת»¯ÎªftpÄܹ»Ê¶±ðÖÐÎĵÄ×Ö·û¼¯ String remotePath; try { remotePath = new String(file.getName().getBytes(StandardCharsets.UTF_8), FTP.DEFAULT_CONTROL_ENCODING); } catch (UnsupportedEncodingException e) { remotePath = file.getName(); } try( InputStream inputStream = new FileInputStream(file); OutputStream outputStream = ftpClient.storeFileStream(remotePath); ){ byte[] buffer = new byte[2048]; int length; while((length = inputStream.read(buffer)) != -1){ outputStream.write(buffer, 0, length); outputStream.flush(); } }catch (Exception e){ } // ¹Ø±ÕÁ÷Ö®ºó±ØÐëÖ´ÐУ¬·ñÔòÏÂÒ»¸öÎļþµ¼ÖÂÁ÷Ϊ¿Õ boolean complete = ftpClient.completePendingCommand(); if(complete){ System.out.println("Îļþ{}ÉÏ´«Íê³É"+ remotePath); }else{ System.out.println("Îļþ{}ÉÏ´«Ê§°Ü"+ remotePath); } } }