📄 test.java
字号:
package cn.edu.sut;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import cn.edu.sut.Path;
import java.util.List;
import java.util.Iterator;
/**
* Class FtpFileUpload.java
* Function 使用ftp上传文件、目录
* @author bwgm
* @version 1.0
* @Date 2007-7-12
*/
public class test {
private FtpClient ftpClient=null;
public test(){
ftpClient=new FtpClient();
ftpClient.setConnectTimeout(5000);//设置连接超时时间为5s
}
/**
* 建立与ftp服务器的连接
* @param host 主机
* @param port 端口
* @param user 用户名
* @param password 密码
* @throws IOException 建立连接失败
* @throws DocumentException
*/
public void connect()throws IOException, DocumentException{
Path path = new Path();
String pathstr = path.getPath("STATISTICXML")+"/test.xml";
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("D:/project/workspace/lg/WEB-INF/src/cn/edu/sut/ftplogin.xml"));
List list = document.selectNodes("/ftplogin/site/host");
Iterator iter=list.iterator();
Element elementts = (Element)iter.next();
String host = elementts.getText();
list = document.selectNodes("/ftplogin/site/port");
iter=list.iterator();
elementts = (Element)iter.next();
String port1 = elementts.getText();
int port = Integer.parseInt(port1);
list = document.selectNodes("/ftplogin/site/user");
iter=list.iterator();
elementts = (Element)iter.next();
String user = elementts.getText();
list = document.selectNodes("/ftplogin/site/password");
iter=list.iterator();
elementts = (Element)iter.next();
String password = elementts.getText();
ftpClient.openServer(host,port);
ftpClient.login(user,password);
ftpClient.binary();
}
/**
* 进入某一级目录
* @param relativePath 相对路径(也可以是绝对路径,但要注意写法)
* @throws IOException 目录不存在或权限不够
*/
public void cd(String relativePath)throws IOException{
ftpClient.cd(relativePath);
}
/**
* 上传整个目录
* @param directory 目录路径
* @throws IOException 目录不存在或访问权限不够
*/
public void uploadDirectory(String directory)throws IOException{
File file=new File(directory);
String name=null;//待上传文件名
if(file.isDirectory()){//如果为目录,则按目录传
File[] files=file.listFiles();
//在ftp服务器上创建对应目录
ftpClient.ascii();
String dir = file.getName();
ftpClient.sendServer("XMKD " + dir + "\r\n");
ftpClient.readServerResponse();
ftpClient.cd(dir);
ftpClient.binary();
//循环传递目录下的所有文件与目录
int i=0;
for(i=0;i<files.length;i++){
File tmpFile=files[i];
if(tmpFile.isDirectory())
uploadDirectory(tmpFile.getAbsolutePath());
else{
name=tmpFile.getName();
upload(directory+"/"+name);
}
}
}
else //如果为文件,则按文件上传
upload(directory,file.getName());
}
/**
* 上传文件
* @param srcFile 源文件
* @throws IOException
*/
public void upload(String srcFile)throws IOException{
File file=new File(srcFile);
FileInputStream fin=new FileInputStream(srcFile);
TelnetOutputStream tos = ftpClient.put(file.getName());
int readLength = 0;
byte[] buf = new byte[1024*1024*50];
while ( (readLength = fin.read(buf)) != -1) {
tos.write(buf, 0, readLength);
}
fin.close();
tos.close();
}
/**
* 上传文件,且重新命名
* @param srcFile 源文件
* @param destFile 目标文件名
* @throws IOException
*/
public void upload(String srcFile,String destFile)throws IOException{
upload(srcFile);
File file=new File(srcFile);
ftpClient.rename(file.getName(), destFile);
}
/**
* 断开与服务器连接
*/
public void close(){
try {
ftpClient.closeServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws IOException, DocumentException {
// TODO Auto-generated method stub
test ftpUpload=new test();
try {
ftpUpload.connect();
ftpUpload.uploadDirectory("D:/xml");
//ftpUpload.upload("D:/xml/电话表改动文档.txt");
ftpUpload.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -