基于 Apache Commons Net 实现javaFTP上传Word文档格式.docx
《基于 Apache Commons Net 实现javaFTP上传Word文档格式.docx》由会员分享,可在线阅读,更多相关《基于 Apache Commons Net 实现javaFTP上传Word文档格式.docx(8页珍藏版)》请在冰豆网上搜索。
我们可以使用第三方提供的包mons。
*mons的包,都有文档,方便使用
*另外IBM也有提供一个ftp包,我没有用过,有兴趣的可以去研究一下
*@commons-net:
*@jakarta-oro:
*@commons-io:
*
@author
我行我素
*@2007-08-03
*/
public
class
MiniFtp{
private
static
Stringusername;
Stringpassword;
Stringip;
int
port;
Propertiesproperty=null;
//配置
StringconfigFile;
//配置文件的路径名
FTPClientftpClient=null;
SimpleDateFormatdateFormat=new
SimpleDateFormat("
yyyy-MM-ddhh:
mm"
);
final
String[]FILE_TYPES={"
文件"
"
目录"
符号链接"
未知类型"
};
void
main(String[]args){
setConfigFile("
woxingwosu.properties"
//设置配置文件路径
connectServer();
listAllRemoteFiles();
//列出所有文件和目录
changeWorkingDirectory("
webroot"
//进入文件夹webroot
listRemoteFiles("
*.jsp"
//列出webroot目录下所有jsp文件
setFileType(FTP.BINARY_FILE_TYPE);
//设置传输二进制文件
uploadFile("
woxingwosu.xml"
myfile.xml"
//上传文件woxingwosu.xml,重新命名为myfile.xml
renameFile("
viewDetail.jsp"
"
newName.jsp"
//将文件viewDetail.jsp改名为newName.jsp
deleteFile("
UpdateData.class"
//删除文件UpdateData.class
loadFile("
UpdateData.java"
loadFile.java"
//下载文件UpdateData.java,并且重新命名为loadFile.java
closeConnect();
//关闭连接
}
*上传文件
*
@param
localFilePath--本地文件路径
newFileName--新的文件名
uploadFile(StringlocalFilePath,StringnewFileName){
//上传文件
BufferedInputStreambuffIn=null;
try{
buffIn=new
BufferedInputStream(new
FileInputStream(localFilePath));
ftpClient.storeFile(newFileName,buffIn);
}catch(Exceptione){
e.printStackTrace();
}finally{
if(buffIn!
=null)
buffIn.close();
*下载文件
remoteFileName--服务器上的文件名
localFileName--本地文件名
loadFile(StringremoteFileName,StringlocalFileName){
//下载文件
BufferedOutputStreambuffOut=null;
buffOut=new
BufferedOutputStream(new
FileOutputStream(localFileName));
ftpClient.retrieveFile(remoteFileName,buffOut);
if(buffOut!
buffOut.close();
*列出服务器上所有文件及目录
listAllRemoteFiles(){
*"
*列出服务器上文件和目录
regStr--匹配的正则表达式
@SuppressWarnings("
unchecked"
)
listRemoteFiles(StringregStr){
FTPFile[]files=ftpClient.listFiles(regStr);
if(files==null||files.length==0)
System.out.println("
Therehasnotanyfile!
else{
TreeSet<
FTPFile>
fileTree=new
TreeSet(
new
Comparator(){
//先按照文件的类型排序(倒排),然后按文件名顺序排序
compare(ObjectobjFile1,ObjectobjFile2){
if(objFile1==null)
return
-1;
else
if(objFile2==null)
1;
FTPFilefile1=(FTPFile)objFile1;
FTPFilefile2=(FTPFile)objFile2;
if(file1.getType()!
=file2.getType())
file2.getType()-file1.getType();
else
file1.getName().compareTo(file2.getName());
);
for(FTPFilefile:
files)
fileTree.add(file);
System.out.printf("
%-35s%-10s%15s%15s\n"
名称"
类型"
修改日期"
大小"
fileTree){
iso8859togbk(file.getName()),FILE_TYPES[file.getType()]
dateFormat.format(file.getTimestamp().getTime()),FileUtils.byteCountToDisplaySize(file.getSize()));
*关闭连接
closeConnect(){
if(ftpClient!
=null){
ftpClient.logout();
ftpClient.disconnect();
*设置配置文件
configFile
setConfigFile(StringconfigFile){
MiniFtp.configFile
=
configFile;
*设置传输文件的类型[文本文件或者二进制文件]
fileType--BINARY_FILE_TYPE、ASCII_FILE_TYPE
setFileType(int
fileType){
ftpClient.setFileType(fileType);
*扩展使用
@return
protected
FTPClientgetFtpClient(){
ftpClient;
*设置参数
configFile--参数的配置文件
setArg(StringconfigFile){
property=new
Properties();
BufferedInputStreaminBuff=null;
inBuff=new
FileInputStream(configFile));
property.load(inBuff);
username=property.getProperty("
username"
password=property.getProperty("
password"
ip=property.getProperty("
ip"
port=Integer.parseInt(property.getProperty("
port"
));
if(inBuff!
inBuff.close();
*连接到服务器
connectServer(){
if
(ftpClient
==
null){
reply;
try
{
setArg(configFile);
ftpClient=new
FTPClient();
ftpClient.setDefaultPort(port);
ftpClient.configure(getFtpConfig());
ftpClient.connect(ip);
ftpClient.login(username,password);
System.out.print(ftpClient.getReplyString());
reply
ftpClient.getReplyCode();
(!
FTPReply.isPositiveCompletion(reply)){
System.err.println("
FTPserverrefusedconnection."
}
catch
(Exceptione){
登录ftp服务器【"
+ip+"
】失败"
*进入到服务器的某个目录下
directory
changeWorkingDirectory(Stringdirectory){
ftpClient.changeWorkingDirectory(directory);
}catch(IOExceptionioe){
ioe.printStackTrace();
*返回到上一层目录
changeToParentDirectory(){
ftpClient.changeToParentDirectory();
*删除文件
deleteFile(Stringfilename){
ftpClient.deleteFile(filename);
*重命名文件
oldFileName--原文件名
newFileName--新文件名
renameFile(StringoldFileName,StringnewFileName){