📄 ftpserver.java
字号:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.CopyStreamListener;
import org.apache.commons.net.io.Util;
public class FtpServer{
// public static Time t = new Time(1);
FTPClient ftp1;
FTPClient ftp2;
FTPClient ftpa;
FTPClient ftpb;
int bufsize = 4096;
//落地下载部分
public void ftptestx(String src,String fname,String srcuname,String srcpwd) throws IOException {
ftp1 = new FTPClient();
String server = "www.wulin.com";
int reply;
try {
//中文识别
ftp1.setControlEncoding("gb2312");
ftp1.connect(src);
System.out.println("Connected to " + src + ".");
reply = ftp1.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp1.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
}
catch (IOException e1) {
System.out.println("e1");
e1.printStackTrace();
}
try {
ftp1.login("anonymous","");
ftp1.setFileType(2);
reply = ftp1.getReplyCode();
}
catch (IOException e2) {
System.out.println("e2");
e2.printStackTrace();
}
//下载
try{
byte[] buf = new byte[bufsize];
File file = new File("tempb.mp3");
RandomAccessFile fout = new RandomAccessFile(file, "rws");
//String filelocation = transconfig.getFileLocation();
InputStream fin = ftp1.retrieveFileStream("娱乐/音乐/杂歌/F.I.R.飞儿乐团-lydia (斗鱼片尾主题曲激情演奏版).mp3");
BufferedInputStream bin = new BufferedInputStream(fin);
int read = bin.read(buf,0,bufsize);
while(read >= 0){
fout.write(buf,0,read);
read = bin.read(buf,0,bufsize);
}
System.out.println("download complete!");
bin.close();
fout.close();
fin.close();
}catch(Exception e3){
System.out.println("e3");
e3.printStackTrace();
}
if (ftp1.isConnected()) {
try {
ftp1.disconnect(); //要断开连接
System.out.println("disconnect form " + src + " success");
}catch(Exception e7){
System.out.println("e7");
e7.printStackTrace();
}
}
}
//本地上传部分
public void ftptests(String dst,String fname,String dstuname,String dstpwd) throws IOException {
ftp2 = new FTPClient();
String server = "www.wulin.com";
int reply;
try {
//中文识别
ftp2.setControlEncoding("gb2312");
ftp2.connect(dst);
System.out.println("Connected to " + dst + ".");
reply = ftp2.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp2.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
}
catch (IOException e4) {
System.out.println("e4");
e4.printStackTrace();
}
try {
ftp2.login("anonymous","");
ftp2.setFileType(2);
System.out.print(ftp2.getReplyString());
reply = ftp2.getReplyCode();
}
catch (IOException e5) {
System.out.println("e5");
e5.printStackTrace();
}
//上传
try{
byte[] buf = new byte[bufsize];
File file2 = new File("tempb.mp3");
RandomAccessFile fin2 = new RandomAccessFile(file2, "rws");
OutputStream fout2 = ftp2.appendFileStream("temp2.mp3");
BufferedOutputStream bout2 = new BufferedOutputStream(fout2);
int read = fin2.read(buf,0,bufsize);
while(read != -1){
bout2.write(buf,0,read);
read = fin2.read(buf,0,bufsize);
bout2.flush();
}
System.out.println("upload complete!");
bout2.close();
fin2.close();
fout2.close();
// 通过URL实现上传
// URL url = new URL("ftp://10.1.168.252/ll.txt");
// URLConnection con = url.openConnection();
// OutputStream o2 = con.getOutputStream();
// o2.write("lanlllllllllllllllllkkkkkkkkkkkkkkkkllllllll".getBytes());
// o2.flush();
// o2.close();
}catch(Exception e6){
System.out.println("e6");
e6.printStackTrace();
}
if (ftp2.isConnected()) {
try {
ftp2.disconnect();//要断开连接
System.out.println("disconnect form " + dst + " success");
}catch(Exception e8){
System.out.println("e8");
e8.printStackTrace();
}
}
}
public void ftptest3(String fname,String src,String srcuname,String srcpwd,String dst,String dstuname,String dstpwd){
ftpa = new FTPClient();
ftpb = new FTPClient();
String server = "www.wulin.com";
int reply1;
int reply2;
try {
//中文识别
ftpa.setControlEncoding("gb2312");
ftpb.setControlEncoding("gb2312");
ftpa.connect(src);
reply1 = ftpa.getReplyCode();
ftpb.connect(dst);
reply2 = ftpb.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply1)||!FTPReply.isPositiveCompletion(reply2)) {
ftpa.disconnect();
ftpb.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
}
catch (IOException exp) {
exp.printStackTrace();
}
try {
ftpa.login("anonymous","");
ftpa.setFileType(2);
reply1 = ftpa.getReplyCode();
ftpb.login("anonymous","");
ftpb.setFileType(2);
reply2 = ftpa.getReplyCode();
}
catch (IOException exp) {
exp.printStackTrace();
}
try{
// byte[] buf = new byte[1024*1024];
// //String filelocation = transconfig.getFileLocation();
// InputStream fin3 = ftpa.retrieveFileStream("娱乐/电影/2005/密林终结者.rmvb");
// BufferedInputStream bin3 = new BufferedInputStream(fin3);
//
// OutputStream fout3 = ftpb.appendFileStream("chy.rmvb");
// BufferedOutputStream bin4 = new BufferedOutputStream(fout3);
// int len;
// len = bin3.read(buf,0,1024*1024);
// long flength = len;
// while(len != -1){
// bin4.write(buf,0,len);
// len = bin3.read(buf,0,1024*1024);
// flength += len ;
// bin4.flush();
//
// }
//
// System.out.println("文件大小= " + (flength+1) + "bits" );
// System.out.println("tranfer file build-in memory complete!");
// bin3.close();
// fout3.close();
// fin3.close();
// bin4.close();
InputStream fin3 = ftpa.retrieveFileStream("娱乐/音乐/杂歌/F.I.R.飞儿乐团-lydia (斗鱼片尾主题曲激情演奏版).mp3");
OutputStream fout3 = ftpb.appendFileStream("test.mp3");
CopyStreamListener csl = null;
// csl.bytesTransferred(111,111,111);;
long size = CopyStreamEvent.UNKNOWN_STREAM_SIZE;
long fsize = Util.copyStream(fin3,fout3,bufsize,size,csl,true);
fin3.close();
fout3.close();
System.out.println("文件大小 = " + fsize + "bytes");
// csl.bytesTransferred(new CopyStreamEvent(){
// System.out.println("test");
// });
}catch(Exception exp3){
System.out.println("exp3");
exp3.printStackTrace();
}
if(ftpa.isConnected()||ftpb.isConnected()){
try {
ftpa.disconnect();//要断开连接
ftpb.disconnect();//要断开连接
System.out.println("disconnect form " + src + " success");
System.out.println("disconnect form " + dst + " success");
}catch(Exception exp4){
System.out.println("exp4");
exp4.printStackTrace();
}
}
}
public static void main(String args[]) throws Exception {
try{
String src = "10.1.168.251";
String dst = "10.1.168.252";
String fname = "";
String srcuname = "anonymous";
String srcpwd = "123456";
String dstuname = "anonymous";
String dstpwd = "123456";
/*
//Xquery解析Xml文件
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
DOMImplementation impl = parser.getDOMImplementation();
System.out.println(">>>>>>>>>>>");
if(!impl.hasFeature("traversal","2.0"))
{
System.out.println("A DOM required!");
//return null;
}
Document doc = parser.parse("file.xml");
DocumentTraversal traversable = (DocumentTraversal) doc;
NodeIterator iterator = traversable.createNodeIterator(doc,NodeFilter.SHOW_TEXT|NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_ATTRIBUTE,null,true);
Node node = doc.getChildNodes().item(0);
String src = node.getChildNodes().item(1).getAttributes().getNamedItem("src").getNodeValue();
String dst = node.getChildNodes().item(1).getAttributes().getNamedItem("dst").getNodeValue();
String srcuname = node.getChildNodes().item(1).getAttributes().getNamedItem("srcuname").getNodeValue();
String srcpwd = node.getChildNodes().item(1).getAttributes().getNamedItem("srcpwd").getNodeValue();
String dstuname = node.getChildNodes().item(1).getAttributes().getNamedItem("dstuname").getNodeValue();
String dstpwd = node.getChildNodes().item(1).getAttributes().getNamedItem("dstpwd").getNodeValue();
String fname = node.getChildNodes().item(1).getAttributes().getNamedItem("fname").getNodeValue();
// System.out.println(src + dst + srcuname + srcpwd + dstuname + dstpwd + fname);
if(srcuname == null)
srcuname = "anonymous";
if(dstuname == null)
dstuname = "anonymous";
*/
FtpServer ft = new FtpServer();
// System.out.println(System.currentTimeMillis());
ft.ftptestx(src,fname,srcuname,srcpwd);
System.out.println(">>>>>>>>>>>>>>>>>>>");
long time1 = System.currentTimeMillis();
ft.ftptests(dst,fname,dstuname,dstpwd);
//// System.out.println(System.currentTimeMillis());
long time2 = System.currentTimeMillis();
System.out.println("本地直接上传共用时间:" + (time2-time1) + "ms");
// System.out.println(">>>>>>>>>>>>>>>>>>>");
long time3 = System.currentTimeMillis();
ft.ftptest3(fname,src,srcuname,srcpwd,dst,dstuname,dstpwd);
long time4 = System.currentTimeMillis();
System.out.println("中转不落地共用时间:" + (time4-time3) + "ms");
}catch(Exception e){
System.out.println("when begin");
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -