| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.core.utils; |
| | | |
| | | import java.io.*; |
| | | import java.util.UUID; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.commons.net.PrintCommandListener; |
| | | 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 org.apache.http.client.methods.CloseableHttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClientBuilder; |
| | | import org.apache.http.util.EntityUtils; |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | /** */ |
| | | /** |
| | | * æ¯ææç¹ç»ä¼ çFTPå®ç¨ç±» |
| | | * |
| | | * @author BenZhou http://www.bt285.cn |
| | | * @version 0.1 å®ç°åºæ¬æç¹ä¸ä¼ ä¸è½½ |
| | | * @version 0.2 å®ç°ä¸ä¼ ä¸è½½è¿åº¦æ±æ¥ |
| | | * @version 0.3 å®ç°ä¸æç®å½å建å䏿æä»¶åå»ºï¼æ·»å 对äºä¸æçæ¯æ |
| | | */ |
| | | @Slf4j |
| | | public class FtpUtil { |
| | | public FTPClient ftpClient = new FTPClient(); |
| | | |
| | | public FtpUtil() { |
| | | // 设置å°è¿ç¨ä¸ä½¿ç¨å°çå½ä»¤è¾åºå°æ§å¶å° |
| | | this.ftpClient.addProtocolCommandListener(new PrintCommandListener( |
| | | new PrintWriter(System.out))); |
| | | } |
| | | |
| | | public FtpUtil(String hostname, int port, String username, String password) |
| | | throws IOException { |
| | | // 设置å°è¿ç¨ä¸ä½¿ç¨å°çå½ä»¤è¾åºå°æ§å¶å° |
| | | connect(hostname, port, username, password); |
| | | } |
| | | |
| | | /** */ |
| | | /** |
| | | * è¿æ¥å°FTPæå¡å¨ |
| | | * |
| | | * @param hostname |
| | | * 主æºå |
| | | * @param port |
| | | * ç«¯å£ |
| | | * @param username |
| | | * ç¨æ·å |
| | | * @param password |
| | | * å¯ç |
| | | * @return æ¯å¦è¿æ¥æå |
| | | * @throws IOException |
| | | */ |
| | | public boolean connect(String hostname, int port, String username, |
| | | String password) throws IOException { |
| | | ftpClient.connect(hostname, port); |
| | | ftpClient.setControlEncoding("GBK"); |
| | | if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { |
| | | if (ftpClient.login(username, password)) { |
| | | return true; |
| | | } |
| | | } |
| | | disconnect(); |
| | | return false; |
| | | } |
| | | |
| | | /** */ |
| | | /** |
| | | * ä»FTPæå¡å¨ä¸ä¸è½½æä»¶,æ¯ææç¹ç»ä¼ ï¼ä¸ä¼ ç¾åæ¯æ±æ¥ |
| | | * |
| | | * @param remote |
| | | * è¿ç¨æä»¶è·¯å¾ |
| | | * @param local |
| | | * æ¬å°æä»¶è·¯å¾ |
| | | * @return ä¸ä¼ çç¶æ |
| | | * @throws IOException |
| | | */ |
| | | public String download(String remote, String local) throws IOException { |
| | | // è®¾ç½®è¢«å¨æ¨¡å¼ |
| | | ftpClient.enterLocalPassiveMode(); |
| | | // 设置以äºè¿å¶æ¹å¼ä¼ è¾ |
| | | ftpClient.setFileType(FTP.BINARY_FILE_TYPE); |
| | | |
| | | String result; |
| | | |
| | | // æ£æ¥è¿ç¨æä»¶æ¯å¦åå¨ |
| | | FTPFile[] files = ftpClient.listFiles(new String( |
| | | remote.getBytes("GBK"), "iso-8859-1")); |
| | | if (files.length != 1) { |
| | | // System.out.println("è¿ç¨æä»¶ä¸åå¨"); |
| | | return "è¿ç¨æä»¶ä¸åå¨"; |
| | | } |
| | | |
| | | long lRemoteSize = files[0].getSize(); |
| | | File f = new File(local); |
| | | // æ¬å°å卿件ï¼è¿è¡æç¹ä¸è½½ |
| | | if (f.exists()) { |
| | | long localSize = f.length(); |
| | | // 夿æ¬å°æä»¶å¤§å°æ¯å¦å¤§äºè¿ç¨æä»¶å¤§å° |
| | | if (localSize >= lRemoteSize) { |
| | | // System.out.println("æ¬å°æä»¶å¤§äºè¿ç¨æä»¶ï¼ä¸è½½ä¸æ¢"); |
| | | // return "æ¬å°æä»¶å¤§äºè¿ç¨æä»¶ï¼ä¸è½½ä¸æ¢"; |
| | | } |
| | | |
| | | // è¿è¡æç¹ç»ä¼ ï¼å¹¶è®°å½ç¶æ |
| | | FileOutputStream out = new FileOutputStream(f, true); |
| | | ftpClient.setRestartOffset(localSize); |
| | | InputStream in = ftpClient.retrieveFileStream(new String(remote |
| | | .getBytes("GBK"), "iso-8859-1")); |
| | | byte[] bytes = new byte[1024]; |
| | | long step = lRemoteSize / 100; |
| | | long process = localSize / step; |
| | | int c; |
| | | while ((c = in.read(bytes)) != -1) { |
| | | out.write(bytes, 0, c); |
| | | localSize += c; |
| | | long nowProcess = localSize / step; |
| | | if (nowProcess > process) { |
| | | process = nowProcess; |
| | | if (process % 10 == 0) |
| | | System.out.println("ä¸è½½è¿åº¦ï¼" + process); |
| | | // TODO æ´æ°æä»¶ä¸è½½è¿åº¦,å¼åæ¾å¨processåéä¸ |
| | | } |
| | | } |
| | | in.close(); |
| | | out.close(); |
| | | boolean isDo = ftpClient.completePendingCommand(); |
| | | if (isDo) { |
| | | result = "300"; |
| | | // result = DownloadStatus.Download_From_Break_Success; |
| | | } else { |
| | | result = "400"; |
| | | // result = DownloadStatus.Download_From_Break_Failed; |
| | | } |
| | | } else { |
| | | OutputStream out = new FileOutputStream(f); |
| | | InputStream in = ftpClient.retrieveFileStream(new String(remote |
| | | .getBytes("GBK"), "iso-8859-1")); |
| | | byte[] bytes = new byte[1024]; |
| | | long step = lRemoteSize / 100; |
| | | long process = 0; |
| | | long localSize = 0L; |
| | | int c; |
| | | while ((c = in.read(bytes)) != -1) { |
| | | out.write(bytes, 0, c); |
| | | localSize += c; |
| | | long nowProcess = localSize / step; |
| | | if (nowProcess > process) { |
| | | process = nowProcess; |
| | | if (process % 10 == 0) |
| | | System.out.println("ä¸è½½è¿åº¦ï¼" + process); |
| | | // TODO æ´æ°æä»¶ä¸è½½è¿åº¦,å¼åæ¾å¨processåéä¸ |
| | | } |
| | | } |
| | | in.close(); |
| | | out.close(); |
| | | boolean upNewStatus = ftpClient.completePendingCommand(); |
| | | if (upNewStatus) { |
| | | result = "500"; |
| | | // result = DownloadStatus.Download_New_Success; |
| | | } else { |
| | | // result = DownloadStatus.Download_New_Failed; |
| | | result = "600"; |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** */ |
| | | /** |
| | | * ä¸ä¼ æä»¶å°FTPæå¡å¨ï¼æ¯ææç¹ç»ä¼ |
| | | * |
| | | * @param local |
| | | * æ¬å°æä»¶åç§°ï¼ç»å¯¹è·¯å¾ |
| | | * @param remote |
| | | * è¿ç¨æä»¶è·¯å¾ï¼ä½¿ç¨/home/directory1/subdirectory/file.extææ¯ |
| | | * http://www.guihua.org /subdirectory/file.ext |
| | | * æç
§Linuxä¸çè·¯å¾æå®æ¹å¼ï¼æ¯æå¤çº§ç®å½åµå¥ï¼æ¯æéå½å建ä¸åå¨çç®å½ç»æ |
| | | * @return ä¸ä¼ ç»æ |
| | | * @throws IOException |
| | | */ |
| | | public String upload(String local, String remote) throws IOException { |
| | | // 设置PassiveModeä¼ è¾ |
| | | ftpClient.enterLocalPassiveMode(); |
| | | |
| | | // 设置以äºè¿å¶æµçæ¹å¼ä¼ è¾ |
| | | ftpClient.setFileType(FTP.BINARY_FILE_TYPE); |
| | | ftpClient.setControlEncoding("GBK"); |
| | | String result; |
| | | // 对è¿ç¨ç®å½çå¤ç |
| | | String remoteFileName = remote; |
| | | if (remote.contains(File.separator)) { |
| | | remoteFileName = remote.substring(remote.lastIndexOf(File.separator) + 1); |
| | | // å建æå¡å¨è¿ç¨ç®å½ç»æï¼åå»ºå¤±è´¥ç´æ¥è¿å |
| | | if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) { |
| | | return "2"; |
| | | } |
| | | } |
| | | |
| | | // æ£æ¥è¿ç¨æ¯å¦å卿件 |
| | | FTPFile[] files = ftpClient.listFiles(new String(remoteFileName |
| | | .getBytes("GBK"), "iso-8859-1")); |
| | | if (files.length == 1) { |
| | | long remoteSize = files[0].getSize(); |
| | | File f = new File(local); |
| | | long localSize = f.length(); |
| | | if (remoteSize == localSize) { |
| | | return "700"; |
| | | // return UploadStatus.File_Exits; |
| | | } else if (remoteSize > localSize) { |
| | | return "800"; |
| | | // return UploadStatus.Remote_Bigger_Local; |
| | | } |
| | | |
| | | // å°è¯ç§»å¨æä»¶å
读åæé,å®ç°æç¹ç»ä¼ |
| | | result = uploadFile(remoteFileName, f, ftpClient, remoteSize); |
| | | |
| | | // 妿æç¹ç»ä¼ 没ææåï¼åå 餿å¡å¨ä¸æä»¶ï¼éæ°ä¸ä¼ |
| | | if (StringUtils.equals(result, "1")) { |
| | | if (!ftpClient.deleteFile(remoteFileName)) { |
| | | return "1"; |
| | | } |
| | | result = uploadFile(remoteFileName, f, ftpClient, 0); |
| | | } |
| | | } else { |
| | | result = uploadFile(remoteFileName, new File(local), ftpClient, 0); |
| | | } |
| | | return result; |
| | | } |
| | | public int getNumFromStr(String str,char searchstr) { |
| | | int count = 0; |
| | | char[] charArray = str.toCharArray(); |
| | | for (char param : charArray) { |
| | | if (param == searchstr) { |
| | | count++; |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | /** */ |
| | | /** |
| | | * ä¸ä¼ æä»¶å°FTPæå¡å¨ï¼æ¯ææç¹ç»ä¼ |
| | | * |
| | | * @param localFile |
| | | * æ¬å°æä»¶æµ |
| | | * @param remote |
| | | * è¿ç¨æä»¶è·¯å¾ï¼ä½¿ç¨/home/directory1/subdirectory/file.extææ¯ |
| | | * http://www.guihua.org /subdirectory/file.ext |
| | | * æç
§Linuxä¸çè·¯å¾æå®æ¹å¼ï¼æ¯æå¤çº§ç®å½åµå¥ï¼æ¯æéå½å建ä¸åå¨çç®å½ç»æ |
| | | * @return ä¸ä¼ ç»æ |
| | | * @throws IOException |
| | | */ |
| | | public String uploadFile(File localFile, String remote) throws IOException { |
| | | // 设置PassiveModeä¼ è¾ |
| | | ftpClient.enterLocalPassiveMode(); |
| | | // 设置以äºè¿å¶æµçæ¹å¼ä¼ è¾ |
| | | ftpClient.setFileType(FTP.BINARY_FILE_TYPE); |
| | | ftpClient.setControlEncoding("GBK"); |
| | | String result; |
| | | // 对è¿ç¨ç®å½çå¤ç |
| | | String remoteFileName = remote ; |
| | | if (remote.contains(File.separator)) { |
| | | remoteFileName = remote.substring(remote.lastIndexOf(File.separator) + 1); |
| | | // å建æå¡å¨è¿ç¨ç®å½ç»æï¼åå»ºå¤±è´¥ç´æ¥è¿å |
| | | if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) { |
| | | return "2"; |
| | | } |
| | | } |
| | | |
| | | // æ£æ¥è¿ç¨æ¯å¦å卿件 |
| | | FTPFile[] files = ftpClient.listFiles(new String(remoteFileName |
| | | .getBytes("GBK"), "iso-8859-1")); |
| | | if (files.length == 1) { |
| | | long remoteSize = files[0].getSize(); |
| | | |
| | | long localSize = localFile.length(); |
| | | if (remoteSize == localSize) { |
| | | return "700"; |
| | | // return UploadStatus.File_Exits; |
| | | } else if (remoteSize > localSize) { |
| | | return "800"; |
| | | // return UploadStatus.Remote_Bigger_Local; |
| | | } |
| | | |
| | | // å°è¯ç§»å¨æä»¶å
读åæé,å®ç°æç¹ç»ä¼ |
| | | result = uploadFile(remoteFileName, localFile, ftpClient, |
| | | remoteSize); |
| | | |
| | | // 妿æç¹ç»ä¼ 没ææåï¼åå 餿å¡å¨ä¸æä»¶ï¼éæ°ä¸ä¼ |
| | | if (StringUtils.equals(result, "1")) { |
| | | if (!ftpClient.deleteFile(remoteFileName)) { |
| | | return "1"; |
| | | } |
| | | result = uploadFile(remoteFileName, localFile, ftpClient, 0); |
| | | } |
| | | // if (result == UploadStatus.Upload_From_Break_Failed) { |
| | | // if (!ftpClient.deleteFile(remoteFileName)) { |
| | | // return UploadStatus.Delete_Remote_Faild; |
| | | // } |
| | | // result = uploadFile(remoteFileName, f, ftpClient, 0); |
| | | // } |
| | | } else { |
| | | result = uploadFile(remoteFileName, localFile, ftpClient, 0); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** */ |
| | | /** |
| | | * æå¼ä¸è¿ç¨æå¡å¨çè¿æ¥ |
| | | * |
| | | * @throws IOException |
| | | */ |
| | | public void disconnect() throws IOException { |
| | | if (ftpClient.isConnected()) { |
| | | ftpClient.disconnect(); |
| | | } |
| | | } |
| | | |
| | | /** */ |
| | | /** |
| | | * éå½å建è¿ç¨æå¡å¨ç®å½ |
| | | * |
| | | * @param remote |
| | | * è¿ç¨æå¡å¨æä»¶ç»å¯¹è·¯å¾ |
| | | * @param ftpClient |
| | | * FTPClient对象 |
| | | * @return ç®å½å建æ¯å¦æå |
| | | * @throws IOException |
| | | */ |
| | | public String CreateDirecroty(String remote, FTPClient ftpClient) |
| | | throws IOException { |
| | | String status = "1"; |
| | | // UploadStatus status = UploadStatus.Create_Directory_Success; |
| | | String directory = remote.substring(0, remote.lastIndexOf(File.separator) + 1); |
| | | if (!directory.equalsIgnoreCase(File.separator) |
| | | && !ftpClient.changeWorkingDirectory(new String(directory |
| | | .getBytes("GBK"), "iso-8859-1"))) { |
| | | // 妿è¿ç¨ç®å½ä¸åå¨ï¼åéå½å建è¿ç¨æå¡å¨ç®å½ |
| | | int start = 0; |
| | | int end = 0; |
| | | if (directory.startsWith(File.separator)) { |
| | | start = 1; |
| | | } else { |
| | | start = 0; |
| | | } |
| | | end = directory |
| | | .indexOf(File.separator, start); |
| | | while (true) { |
| | | String subDirectory = new String(remote.substring(start, end) |
| | | .getBytes("GBK"), "iso-8859-1"); |
| | | if (!ftpClient.changeWorkingDirectory(subDirectory)) { |
| | | if (ftpClient.makeDirectory(subDirectory)) { |
| | | ftpClient.changeWorkingDirectory(subDirectory); |
| | | } else { |
| | | System.out.println("å建ç®å½å¤±è´¥"); |
| | | return "2"; |
| | | // return UploadStatus.Create_Directory_Fail; |
| | | } |
| | | } |
| | | |
| | | start = end + 1; |
| | | end = directory.indexOf(File.separator, |
| | | start); |
| | | |
| | | // æ£æ¥ææç®å½æ¯å¦åå»ºå®æ¯ |
| | | if (end <= start) { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return status; |
| | | } |
| | | |
| | | /** |
| | | * ä¸ä¼ æä»¶å°æå¡å¨,æ°ä¸ä¼ åæç¹ç»ä¼ |
| | | * |
| | | * @param remoteFile |
| | | * è¿ç¨æä»¶åï¼å¨ä¸ä¼ ä¹åå·²ç»å°æå¡å¨å·¥ä½ç®å½åäºæ¹å |
| | | * @param localFile |
| | | * æ¬å°æä»¶File奿ï¼ç»å¯¹è·¯å¾ |
| | | * éè¦æ¾ç¤ºçå¤çè¿åº¦æ¥è¿å¼ |
| | | * @param ftpClient |
| | | * FTPClientå¼ç¨ |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public String uploadFile(String remoteFile, File localFile, |
| | | FTPClient ftpClient, long remoteSize) throws IOException { |
| | | String status; |
| | | // æ¾ç¤ºè¿åº¦çä¸ä¼ |
| | | long step = localFile.length() / 100; |
| | | step=step ==0?1:step; |
| | | long process = 0; |
| | | long localreadbytes = 0L; |
| | | // System.out.println(remoteFile +"-------------------------"+localFile.getPath().toString()); |
| | | RandomAccessFile raf = new RandomAccessFile(localFile, "r"); |
| | | OutputStream out = ftpClient.appendFileStream(new String(remoteFile |
| | | .getBytes("GBK"), "iso-8859-1")); |
| | | // æç¹ç»ä¼ |
| | | if (remoteSize > 0) { |
| | | ftpClient.setRestartOffset(remoteSize); |
| | | process = remoteSize / step; |
| | | raf.seek(remoteSize); |
| | | localreadbytes = remoteSize; |
| | | } |
| | | byte[] bytes = new byte[1024]; |
| | | int c; |
| | | while ((c = raf.read(bytes)) != -1) { |
| | | out.write(bytes, 0, c); |
| | | localreadbytes += c; |
| | | if (localreadbytes / step != process) { |
| | | process = localreadbytes / step; |
| | | System.out.println("ä¸ä¼ è¿åº¦:" + process); |
| | | // TODO æ±æ¥ä¸ä¼ ç¶æ |
| | | } |
| | | } |
| | | out.flush(); |
| | | raf.close(); |
| | | out.close(); |
| | | boolean result = ftpClient.completePendingCommand(); |
| | | if (remoteSize > 0) { |
| | | status = result ? "0" : "1"; |
| | | // status = result ? UploadStatus.Upload_From_Break_Success |
| | | // : UploadStatus.Upload_From_Break_Failed; |
| | | } else { |
| | | status = result ? "0" : "1"; |
| | | // status = result ? UploadStatus.Upload_New_File_Success |
| | | // : UploadStatus.Upload_New_File_Failed; |
| | | } |
| | | return status; |
| | | } |
| | | |
| | | public byte[] getOnlineInputsteam(String url){ |
| | | try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { |
| | | HttpGet httpGet = new HttpGet(url); |
| | | CloseableHttpResponse resp = httpClient.execute(httpGet);// è°ç¨æå¡å¨æ¥å£ |
| | | // return resp.getEntity().getContent(); |
| | | byte[] data = EntityUtils.toByteArray(resp.getEntity());// å°è¿åçå¾çæè
æä»¶è½¬åæåèæ°ç»çå½¢å¼ |
| | | // BASE64Encoder encoder = new BASE64Encoder(); |
| | | // String imageBase64 = encoder.encode(data); |
| | | // String imageBase64 = "data:image/jpg;base64," + encoder.encodeBuffer(data).trim(); |
| | | // imageBase64 = imageBase64.replaceAll("\n", "").replaceAll("\r", "").replaceAll(" ", "");//å é¤ \r\n |
| | | return data; |
| | | } catch (Exception e) { |
| | | log.error("è·åå¾çå¼å¸¸:{}",e.getMessage()); |
| | | } |
| | | return null; |
| | | } |
| | | public boolean uploadOnlineFile(String url,String ftpPath, String fileName) throws Exception |
| | | { |
| | | byte[] buf = getOnlineInputsteam(url); |
| | | if(buf == null){ |
| | | return false; |
| | | } |
| | | try |
| | | { |
| | | ftpClient.enterLocalPassiveMode();// å¼å¯è¢«å¨æ¨¡å¼ |
| | | ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); |
| | | CreateDirecroty( ftpPath,ftpClient); |
| | | String printWorkingDirectory = ftpClient.printWorkingDirectory(); |
| | | System.out.println("è·åå½åç®å½:" + printWorkingDirectory); |
| | | boolean flag = ftpClient.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), new ByteArrayInputStream(buf)); |
| | | // éè¿åé QUIT å½ä»¤æ³¨é FTP æå¡å¨ |
| | | ftpClient.logout(); |
| | | return flag; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | e.printStackTrace(); |
| | | } |
| | | finally |
| | | { |
| | | if (ftpClient.isConnected()){ |
| | | ftpClient.disconnect(); |
| | | } |
| | | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * å é¤ä¸ä¸ªæä»¶ |
| | | */ |
| | | public boolean deleteFile(String filename) { |
| | | boolean flag = true; |
| | | try { |
| | | |
| | | flag = ftpClient.deleteFile(filename); |
| | | if (flag) { |
| | | System.out.println("å 餿件æåï¼"); |
| | | } else { |
| | | System.out.println("å é¤æä»¶å¤±è´¥ï¼"); |
| | | } |
| | | } catch (IOException ioe) { |
| | | ioe.printStackTrace(); |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | try { |
| | | FtpUtil ftpUtil = new FtpUtil("175.27.187.84", 21, "ftpuser","doumee168" ); |
| | | ftpUtil.uploadOnlineFile("https://profile-avatar.csdnimg.cn/default.jpg!1", "test"+File.separator, UUID.randomUUID().toString()+"test.jpg"); |
| | | /*FtpUtil myFtp = new FtpUtil("106.15.54.228", 21, "ftptlg", |
| | | "tlg168.com"); |
| | | System.out.println(myFtp.upload("D:\\devices.sql", "/1111/devices.sql")); |
| | | |
| | | // myFtp.ftpClient.makeDirectory(new String("çµè§å§".getBytes("GBK"), |
| | | // "iso-8859-1")); |
| | | // myFtp.ftpClient.changeWorkingDirectory(new String("çµè§å§" |
| | | // .getBytes("GBK"), "iso-8859-1")); |
| | | // myFtp.ftpClient.makeDirectory(new String("走西å£".getBytes("GBK"), |
| | | // "iso-8859-1")); |
| | | // System.out.println(myFtp.upload("http://www.5a520.cn /yw.flv", |
| | | // "/yw.flv", 5)); |
| | | // System.out.println(myFtp.upload("http://www.5a520.cn /走西å£24.mp4", |
| | | // "/央è§èµ°è¥¿å£/æ°æµªç½/走西å£24.mp4")); |
| | | *//** |
| | | * t.connect("pengtu", "61.191.61.226", 2122, "yangnet", |
| | | * "yanglong@doumee.com"); File file = new File( |
| | | * "D:\\lib/commons-net-3.3-bin/commons-net-3.3/NOTICE.txt"); |
| | | * |
| | | * |
| | | *//* |
| | | |
| | | System.out.println(myFtp.download("/1/2/NOTICE.txt", "D:\\å.txt")); |
| | | |
| | | myFtp.deleteFile("/1/2/NOTICE.txt"); |
| | | myFtp.disconnect(); |
| | | |
| | | System.out.println("javaçæ¬å·ï¼" + System.getProperty("java.version")); // javaçæ¬å· |
| | | System.out |
| | | .println("Javaæä¾ååç§°ï¼" + System.getProperty("java.vendor")); // Javaæä¾ååç§° |
| | | System.out.println("Javaæä¾åç½ç«ï¼" |
| | | + System.getProperty("java.vendor.url")); // Javaæä¾åç½ç« |
| | | System.out.println("jreç®å½ï¼" + System.getProperty("java.home")); // Javaï¼å¦ï¼åºè¯¥æ¯jreç®å½ |
| | | System.out.println("Javaèææºè§èçæ¬å·ï¼" |
| | | + System.getProperty("java.vm.specification.version")); // Javaèææºè§èçæ¬å· |
| | | System.out.println("Javaèææºè§èæä¾åï¼" |
| | | + System.getProperty("java.vm.specification.vendor")); // Javaèææºè§èæä¾å |
| | | System.out.println("Javaèææºè§èåç§°ï¼" |
| | | + System.getProperty("java.vm.specification.name")); // Javaèææºè§èåç§° |
| | | System.out.println("Javaèææºçæ¬å·ï¼" |
| | | + System.getProperty("java.vm.version")); // Javaèææºçæ¬å· |
| | | System.out.println("Javaèææºæä¾åï¼" |
| | | + System.getProperty("java.vm.vendor")); // Javaèææºæä¾å |
| | | System.out.println("Javaèææºåç§°ï¼" |
| | | + System.getProperty("java.vm.name")); // Javaèææºåç§° |
| | | System.out.println("Javaè§èçæ¬å·ï¼" |
| | | + System.getProperty("java.specification.version")); // Javaè§èçæ¬å· |
| | | System.out.println("Javaè§èæä¾åï¼" |
| | | + System.getProperty("java.specification.vendor")); // Javaè§èæä¾å |
| | | System.out.println("Javaè§èåç§°ï¼" |
| | | + System.getProperty("java.specification.name")); // Javaè§èåç§° |
| | | System.out.println("Javaç±»çæ¬å·ï¼" |
| | | + System.getProperty("java.class.version")); // Javaç±»çæ¬å· |
| | | System.out.println("Java类路å¾ï¼" |
| | | + System.getProperty("java.class.path")); // Javaç±»è·¯å¾ |
| | | System.out.println("Java libè·¯å¾ï¼" |
| | | + System.getProperty("java.library.path")); // Java libè·¯å¾ |
| | | System.out.println("Javaè¾å
¥è¾åºä¸´æ¶è·¯å¾ï¼" |
| | | + System.getProperty("java.io.tmpdir")); // Javaè¾å
¥è¾åºä¸´æ¶è·¯å¾ |
| | | System.out |
| | | .println("Javaç¼è¯å¨ï¼" + System.getProperty("java.compiler")); // Javaç¼è¯å¨ |
| | | System.out.println("Javaæ§è¡è·¯å¾ï¼" |
| | | + System.getProperty("java.ext.dirs")); // Javaæ§è¡è·¯å¾ |
| | | System.out.println("æä½ç³»ç»åç§°ï¼" + System.getProperty("os.name")); // æä½ç³»ç»åç§° |
| | | System.out.println("æä½ç³»ç»çæ¶æï¼" + System.getProperty("os.arch")); // æä½ç³»ç»çæ¶æ |
| | | System.out.println("æä½ç³»ç»çæ¬å·ï¼" + System.getProperty("os.version")); // æä½ç³»ç»çæ¬å· |
| | | System.out.println("æä»¶åé符ï¼" + System.getProperty("file.separator")); // æä»¶åé符 |
| | | System.out.println("è·¯å¾åé符ï¼" + System.getProperty("path.separator")); // è·¯å¾åé符 |
| | | System.out.println("ç´çº¿åé符ï¼" + System.getProperty("line.separator")); // ç´çº¿åé符 |
| | | System.out.println("æä½ç³»ç»ç¨æ·åï¼" + System.getProperty("user.name")); // ç¨æ·å |
| | | System.out.println("æä½ç³»ç»ç¨æ·ç主ç®å½ï¼" + System.getProperty("user.home")); // ç¨æ·ç主ç®å½ |
| | | System.out.println("å½åç¨åºæå¨ç®å½ï¼" + System.getProperty("user.dir")); // å½åç¨åºæå¨ç®å½*/ |
| | | |
| | | } catch (IOException e) { |
| | | System.out.println("è¿æ¥FTPåºéï¼" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /*** |
| | | * @ä¸ä¼ æä»¶å¤¹ |
| | | * @param localDirectory |
| | | * å½å°æä»¶å¤¹ |
| | | * @param remoteDirectoryPath |
| | | * Ftp æå¡å¨è·¯å¾ 以ç®å½File.separatorç»æ |
| | | * */ |
| | | public boolean uploadDirectory(String localDirectory, |
| | | String remoteDirectoryPath) throws Exception { |
| | | File src = new File(localDirectory); |
| | | try { |
| | | remoteDirectoryPath = remoteDirectoryPath + File.separator; |
| | | |
| | | // System.out.println(remoteDirectoryPath+":===============å建ç®å½================="); |
| | | boolean makeDirFlag = this.ftpClient.makeDirectory(remoteDirectoryPath); |
| | | }catch (IOException e) { |
| | | e.printStackTrace(); |
| | | log.info(remoteDirectoryPath + "ç®å½å建失败"); |
| | | return false; |
| | | } |
| | | File[] allFile = src.listFiles(); |
| | | for (int currentFile = 0;currentFile < allFile.length;currentFile++) { |
| | | File f =allFile[currentFile]; |
| | | String srcName= f.getPath().toString(); |
| | | // System.out.println(srcName+":====================读åæä»¶======================"); |
| | | String tPath = remoteDirectoryPath+f.getName(); |
| | | if (!f.isDirectory()) { |
| | | |
| | | // System.out.println(srcName+":====================å¼å§ä¼ è¾æä»¶======================"); |
| | | int dNum = getNumFromStr(tPath,File.separator.toCharArray()[0]); |
| | | uploadFile(f, tPath); |
| | | if(dNum -2>=0){ |
| | | for (int i = 0; i < dNum-1; i++) { |
| | | this.ftpClient.changeToParentDirectory(); |
| | | } |
| | | } |
| | | }else{ |
| | | // System.out.println(srcName+":====================å建åç®å½======================"); |
| | | uploadDirectory(srcName, |
| | | tPath+File.separator); |
| | | } |
| | | // System.out.println(srcName+":====================ä¸ä¼ 宿¯======================"); |
| | | } |
| | | return true; |
| | | } |
| | | /*** |
| | | * @ä¸ä¼ æä»¶å¤¹ |
| | | * @param pathName |
| | | * æä»¶å¤¹ |
| | | * */ |
| | | public boolean delDirectory(String pathName ) throws Exception { |
| | | try { |
| | | this.ftpClient.changeWorkingDirectory( pathName.substring(0, pathName.lastIndexOf(File.separator)) ); |
| | | ftpClient.removeDirectory(pathName); |
| | | }catch (IOException e) { |
| | | e.printStackTrace(); |
| | | log.info(pathName + "ç®å½å é¤å¤±è´¥"); |
| | | return false; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | } |