JDK1.7以上javaFTP上傳刪除文件的實(shí)現(xiàn)方法
實(shí)例如下:
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從頁面得到的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存儲的路徑
*
@param fileName 要刪除的文件名稱
*
@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端口號 FTP_PORT=1234 #用戶名 FTP_USER=yanzhou #密碼 FTP_PASSWORD=abcdefg12345 #FTP賬號目錄 FTP_REMOTE_FILEPATH=
以上這篇JDK1.7以上javaFTP上傳刪除文件的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 實(shí)現(xiàn)配置文件加解密原理
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)配置文件加解密原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java8 String內(nèi)存優(yōu)化之字符串常量池詳解
這篇文章主要介紹了Java8 String內(nèi)存優(yōu)化之字符串常量池,具有很好的參考價(jià)值,希望對大家有所幫助,2023-07-07
Java經(jīng)驗(yàn)點(diǎn)滴:類注釋文檔編寫方法
Java經(jīng)驗(yàn)點(diǎn)滴:類注釋文檔編寫方法...2006-12-12
Mybatis -如何處理clob類型數(shù)據(jù)
這篇文章主要介紹了Mybatis 如何處理clob類型數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Jdbctemplate多數(shù)據(jù)源配置方法詳解
這篇文章主要介紹了Jdbctemplate多數(shù)據(jù)源配置方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
MyBatisPlus條件構(gòu)造器的實(shí)現(xiàn)示例
本文主要介紹了MyBatisPlus條件構(gòu)造器的實(shí)現(xiàn)示例,主要包括了QueryWrapper,UpdateWrapper,LambdaQueryWrapper,LambdaUpdateWrapper這四種,具有一定的參考價(jià)值,感興趣的可以了解下2023-12-12
通過Spring AOP實(shí)現(xiàn)異常捕捉機(jī)制
在開發(fā)過程中,異常處理是一個(gè)不可忽視的重要環(huán)節(jié),合理、優(yōu)雅地處理異常不僅能提高代碼的魯棒性,還能提升系統(tǒng)的用戶體驗(yàn),本文將介紹如何通過Spring AOP實(shí)現(xiàn)一個(gè)高效的異常捕捉機(jī)制,使得異常處理變得更加優(yōu)雅和統(tǒng)一,需要的朋友可以參考下2024-08-08

