實(shí)例如下:
專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)杭錦免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
packagecom.itv.launcher.util; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importjava.net.InetSocketAddress; importjava.util.Properties; importjava.util.StringTokenizer; importsun.net.TelnetOutputStream; importsun.net.ftp.FtpClient; importsun.net.ftp.FtpProtocolException; /** * FTP上傳工具類 * * @author yanzhou * @version v1.0 */ publicclass FTPUtil { privatestatic FtpClient ftpClient = null; privatestatic final String url; privatestatic final int port; privatestatic final String user; privatestatic final String password; privatestatic final String remoteFilePath; static{ Properties FTPPro = ReadFTPProperties.getMsgFromPro(); url = FTPPro.getProperty("FTP_URL"); port = Integer.parseInt(FTPPro.getProperty("FTP_PORT")); user = FTPPro.getProperty("FTP_USER"); password = FTPPro.getProperty("FTP_PASSWORD"); remoteFilePath = FTPPro.getProperty("FTP_REMOTE_FILEPATH"); } /** * 鏈接FTP * @throws FtpProtocolException */ privatestatic void connectFTP() throwsFtpProtocolException { try{ ftpClient = FtpClient.create(); ftpClient.connect(newInetSocketAddress(url, port)); ftpClient.login(user, password.toCharArray()); ftpClient.setBinaryType(); if(!"".equals(remoteFilePath) && remoteFilePath != null) { ftpClient.changeDirectory(remoteFilePath); } }catch(IOException e) { e.printStackTrace(); } } /** * 關(guān)閉FTP鏈接 */ publicstatic void closeFTP() { try{ if(ftpClient != null) { ftpClient.close(); } }catch(IOException e) { e.printStackTrace(); } } /** * 上傳文件到FTP * @param file file文件,struts2從頁(yè)面得到的File類型 * @param filePath 要保存在FTP上的路徑(文件夾) * @param fileName 文件名(test001.jpg) * @return 文件是否上傳成功 * @throws Exception */ publicstatic boolean upload(File file, String filePath, String fileName) { TelnetOutputStream to = null; FileInputStream fi = null; filePath = remoteFilePath + Constants.FILE_SEPARATOR + filePath; try{ if(file != null) { connectFTP(); if(!isDirExist(filePath.replace("\\","/"))) { createDir(filePath.replace("\\","/")); ftpClient.changeDirectory(filePath.replace("\\","/")); } fi = newFileInputStream(file); to = (TelnetOutputStream) ftpClient.putFileStream(fileName, true); byte[] bytes = newbyte[1024]; inti = fi.read(bytes); while(i != -1) { to.write(bytes); i = fi.read(bytes); } } returntrue; }catch(FileNotFoundException e1) { returnfalse; }catch(IOException e2) { returnfalse; }catch(Exception e) { returnfalse; }finally{ if(fi != null) { try{ fi.close(); }catch(IOException e) { e.printStackTrace(); } } if(to != null) { try{ to.flush(); to.close(); }catch(IOException e) { e.printStackTrace(); } } closeFTP(); } } /** * 刪除FTP制定目錄下的文件 * @param filePath 文件在FTP存儲(chǔ)的路徑 * @param fileName 要?jiǎng)h除的文件名稱 * @return 是否刪除成功 */ publicstatic boolean deleteFileFtp(String filePath, String fileName){ try{ connectFTP(); filePath = remoteFilePath + Constants.FILE_SEPARATOR + filePath + Constants.FILE_SEPARATOR; if(!isDirExist(filePath.replace("\\","/"))) { returnfalse; } ftpClient.changeDirectory(filePath.replace("\\","/")); ftpClient.deleteFile(fileName); returntrue; }catch(Exception e) { e.printStackTrace(); returnfalse; }finally{ closeFTP(); } } /** * 檢查文件夾是否存在 * * @param dir * @param ftpClient * @return */ privatestatic Boolean isDirExist(String dir) { try{ ftpClient.changeDirectory(dir); }catch(Exception e) { returnfalse; } returntrue; } /** * 創(chuàng)建文件夾 * * @param dir * @param ftpClient * @throws Exception */ privatestatic void createDir(String dir) throwsException { ftpClient.setAsciiType(); StringTokenizer s = newStringTokenizer(dir, "/");// sign s.countTokens(); String pathName = ""; while(s.hasMoreElements()) { pathName = pathName + "/"+ (String) s.nextElement(); try{ ftpClient.makeDirectory(pathName); }catch(Exception e) { e = null; } } ftpClient.setBinaryType(); } }
2. 常量類,系統(tǒng)的路徑分隔符
packagecom.itv.launcher.util; publicinterface Constants { //路徑分隔符 publicstatic String FILE_SEPARATOR = System.getProperty("file.separator"); }
3. FTP鏈接的配置properties文件,包括用戶名密碼一些信息
#FTP的IP地址 FTP_URL=127.0.0.1 #FTP端口號(hào) FTP_PORT=1234 #用戶名 FTP_USER=yanzhou #密碼 FTP_PASSWORD=abcdefg12345 #FTP賬號(hào)目錄 FTP_REMOTE_FILEPATH=
以上這篇JDK1.7以上javaFTP上傳刪除文件的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。