java做的比较完善的FTP上传下载文件服务器源码
Filename: ftp.java
Author: leetsing(elove)
Create date: 2004-08-30
Use: connect to FTP server,then upload and download file
Modify date: 2004-09-05 add to upload file
2004-09-13 add to download file
Copy right: Magisky Media Technology Co.,Ltd. *******************************************************************************************************
字串7 */
//import cz.dhl.io.*;
//import cz.dhl.ftp.*;
import sun.net.ftp.*;
import sun.net.*;
import java.applet.*;
import java.io.*;
import java.io.IOException;
import java.util.StringTokenizer;
import sun.net.ftp.FtpClient;
import java.util.ArrayList; public class ftp extends Applet
{
FtpClient aftp;
DataOutputStream outputs ;
TelnetInputStream ins;
TelnetOutputStream outs; int ch;
public String a;
String hostname="";
private String path = "/"; public static void main(String[] args)
{
String hostname = "192.168.0.56";
int port = 2121;
String uid = "lee";
String pwd = "lee";
String RWFileDir = "D:\\smsftp\\";//文件目录 //连接ftp服务器
ftp ft = new ftp();
ft.connect(RWFileDir,hostname,port,uid,pwd);
字串3 //下载文件
if (ft.aftp != null){
try {
ft.getNameList(RWFileDir);
}catch(IOException e)
{
System.out.println("下载文件出错:"+e);
}
} //上传文件
if (ft.aftp != null){
String sdir = RWFileDir + "subunsubfromsp\\";
File fdir = new File(sdir);
String FileName = ""; for(int i=0;i FileName = sdir + (fdir.list())[i]; ft.uploadFile(RWFileDir,FileName);
}
//System.out.println("成功上传的文件:");
//ft.showFileContents("subunsubfromsp\\");
} //删除subunsubfromsp目录下已经上传的文件文件 字串6
// ft.deleFile(RWFileDir); //断开服务器连接
ft.stop(RWFileDir); } public FtpClient connect(String RWFileDir,String hostname,int port,String uid,String pwd)
{
this.hostname = hostname;
System.out.println("正在连接"+hostname+",请等待.....");
try{
aftp = new FtpClient(hostname,port);
aftp.login(uid,pwd);
aftp.binary();
//aftp.openPortDataConnection();
a = "连接主机:"+hostname+"成功!";
System.out.println(a);
}
catch(FtpLoginException e){
a="登陆主机:"+hostname+"失败!请检查用户名或密码是否正确:"+e;
System.out.println(a);
//return false;
}
catch (IOException e){
a="连接主机:"+hostname+"失败!请检查端口是否正确:"+e; 字串3
System.out.println(a);
//return false;
}
catch(SecurityException e)
{
a="无权限与主机:"+hostname+"连接!请检查是否有访问权限:"+e;
System.out.println(a);
//return false;
} log(RWFileDir,a);
return aftp;
} public void stop(String RWFileDir)
{
String message = "";
try {
if(aftp!=null){
aftp.closeServer();
message = "与主机"+hostname+"连接已断开!";
System.out.println(message);
log(RWFileDir,message);
}
}
catch(IOException e)
{
message = "与主机"+hostname+"断开连接失败!"+e; 字串2
System.out.println(message);
log(RWFileDir,message);
}
} public boolean downloadFile(String RWFileDir,String filepathname){
boolean result=true;
String message = "";
if (aftp != null)
{
System.out.println("正在下载文件"+filepathname+",请等待....");
String badfile = filepathname.substring(filepathname.length()-4,filepathname.length());
String badlog = filepathname.substring(filepathname.length()-7,filepathname.length());
String baddir = "";
if ((badfile.compareTo(".bad") != 0) && (badlog.compareTo(".badlog") != 0)){
baddir = "subunsubtosp\\";
}
else{
baddir = "bad\\";
} 字串9
String strdir = "subunsubtosp\\";
//System.out.println(RWFileDir + baddir + filepathname);
try{
//FtpClient fc=new FtpClient("192.168.0.56",2121);
//fc.login("lee","lee");
int ch;
File fi = new File(RWFileDir + baddir + filepathname);
//aftp.cd(strdir);
RandomAccessFile getFile = new RandomAccessFile(fi,"rw");
getFile.seek(0);
TelnetInputStream fget=aftp.get(strdir+filepathname);
DataInputStream puts = new DataInputStream(fget);
while ((ch = puts.read()) >= 0) {
getFile.write(ch); }
//s.delete(); 字串1 fget.close();
getFile.close();
//fc.closeServer(); message = "下载"+filepathname+"文件到"+baddir +"目录成功!";
System.out.println(message);
log(RWFileDir,message);
}
catch(IOException e){
message = "下载"+filepathname+"文件到"+baddir +"目录失败!"+e;
System.out.println(message);
log(RWFileDir,message);
result = false ; }
}
else{
result = false;
}
return result;
} public boolean uploadFile(String RWFileDir,String filepathname){
boolean result=true; 字串8
String message = "";
if (aftp != null)
{
System.out.println("正在上传文件"+filepathname+",请等待...."); try{
String fg =new String("\\subunsubfromsp\\");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index+1);
File localFile = new File(filepathname) ; RandomAccessFile sendFile = new RandomAccessFile(filepathname,"r");
//
sendFile.seek(0);
//改名上传temp_
filename = filename.substring(0,15)+"temp_"+filename.substring(15,filename.length());
outs = aftp.put(filename);
outputs = new DataOutputStream(outs); 字串8
while (sendFile.getFilePointer() < sendFile.length() )
{
ch = sendFile.read();
outputs.write(ch);
} rename(filename.substring(15,filename.length()),filename.substring(20,filename.length()));
outs.close();
sendFile.close(); message = "上传"+filepathname+"文件成功!";
System.out.println(message);
log(RWFileDir,message);
}
catch(IOException e){
message = "上传"+filepathname+"文件失败!"+e;
System.out.println(message);
log(RWFileDir,message);
result = false ; 字串1 }
}
else{
result = false;
}
return result;
} public void rename(String oldName,String newName){ //aftp.renameTo(oldName,newName);
File Old = new File(oldName); //oldName
File New = new File(newName); //newName
//aftp.renameTo(New);
//boolean Old.renameTo(File newName);
//System.out.println(Old);
//System.out.println(New); } public static void deleFile(String RWFileDir) {
//try {
//取得ReadFile目录下的txt文件
String sdir = RWFileDir + "subunsubfromsp\\";
File fdir = new File(sdir);
String FileName = "";
int j = fdir.list().length; System.out.println(sdir+"目录下要删除的文件数:"+fdir.list().length);
字串6 File file;
for(int i=0;i {
//删除subunsubfromsp中的txt文件
FileName = RWFileDir + "subunsubfromsp\\" + (fdir.list())[0];
file = new File(FileName);
file.delete();
System.out.println("已经成功删除"+FileName+"文件!");
}
java做的比较完善的FTP上传下载文件服务器源码的更多相关文章
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...
- Java.ftp上传下载
1:jar的maven的引用: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- java客户端调用ftp上传下载文件
1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...
- 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)
前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...
- ftp上传下载工具类
package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...
- windows系统下ftp上传下载和一些常用命令
先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...
- windows下ftp上传下载和一些常用命令
先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...
- FTP上传下载工具(FlashFXP) v5.5.0 中文版
软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...
- C# -- FTP上传下载
C# -- FTP上传下载 1. C#实现FTP下载 private static void TestFtpDownloadFile(string strFtpPath, string strFile ...
随机推荐
- Nginx和Apache配置日志格式记录Cookie
记录Cookie有什么用? 有时候我们需要通过web服务器的访问日志来统计UV(独立访客),并据此分析用户的行为.而UV是依据cookie数据得出的统计.UV相对于IP的好处是:IP是一个反映网络虚拟 ...
- rabbitmq 重复ACK导致消息丢失
rabbitmq 重复确认导致消息丢失 背景 rabbitmq 在应用场景中,大多采用工作队列 work-queue的模式. 在一个常见的工作队列模式中,消费者 worker 将不断的轮询从队列中拉取 ...
- 如何给Visual Studio 2015安装XNA4.0
从这个地址下载安装包 https://mxa.codeplex.com/releases/view/618279 按照说明步骤依次安装 DirectX XNA Framework 4.0 Redist ...
- IE9 使用document.getElementsByName("abc") 不能获取到名称相同SPAN元素
<div name="abc"></div> <input name="abc" type="text" /& ...
- Javascript模式(第一章简介)------读书笔记
一:模式 模式是一个通用问题的解决方案,可以提供一个更好的实践经验.有用的抽象化表示和解决一类问题的模板. 本书主要讨论如下三种类型的模式 1 设计模式:可复用面向对象软件的基础,包括singleto ...
- VR外包团队:VR和AR技术已经红得发紫
近6个月以来,VR和AR技术已经红得发紫. 不管是创业公司还是互联网巨头,如果不在VR领域有所涉猎,都不好意思跟外界打招呼.最近,阿里巴巴公布VR战略并推出了一条VR购物的视频,更是给业界打了满满一碗 ...
- flash flex 程序出现错误 Error #2032
解决思路参考: http://www.cnblogs.com/enjoyprogram/archive/2012/06/21/2557615.html 有可能是这种情况: 状况:在安装flshbuil ...
- 扫描二维码自动识别手机系统(Android/IOS)
移动互联网发展迅速,各种APP的开发都会推出多个版本(多终端),比如:iPhone版.iPad版.Android版.有些APP还会考虑覆盖到多个国家(国际化),比如:中文版.英文版.日文版.韩文版等. ...
- 数据挖掘之决策树ID3算法(C#实现)
决策树是一种非常经典的分类器,它的作用原理有点类似于我们玩的猜谜游戏.比如猜一个动物: 问:这个动物是陆生动物吗? 答:是的. 问:这个动物有鳃吗? 答:没有. 这样的两个问题顺序就有些颠倒,因为一般 ...
- Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...