我有一段ftp上传的代码:import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class testFtp
{
private static FtpClient ftpClient;
public static boolean connectServer(String ip, int port,String user, String password)
{
try
{
ftpClient = new FtpClient();
ftpClient.openServer(ip,port);
ftpClient.login(user,password);
System.out.println("login success!");
return true;
}
catch (IOException ex)
{
System.out.println(ex);
return false;
}
}public static void closeConnect()
{
try
{
ftpClient.closeServer();
}
catch (IOException ex)
{
System.out.println("not disconnect");
System.out.println(ex);
}
}public static boolean upload(File localfilename,String remotefilename,String ftppath)
{
if (ftppath.length() != 0)
{
try
{
ftpClient.cd(ftppath);
/*这是在服务器上建文件夹
String new_dir="20070810";
String cmd_mkdir="MKD "+new_dir+"\r\n;
ftpClient.sendServer(cmd_mkdir);
ftpClient.cd(new_dir);*/
//现在的问题是,如果我不在上传的时候建文件夹,则能上传成功
//一旦建立文件夹,再上传的话,报错:
//sun.net.ftp.FtpProtocolException: PORT :500 'EPRT
//|1|172.18.114.222|4467|': command not understood ---------------(1)
}
catch (IOException ex)
{
System.out.println(ex);
}
}
try
{
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(remotefilename);
File file_in = localfilename;
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
os.write(bytes, 0, c);
is.close();
os.close();
System.out.println("upload success");
return true;
}
catch (IOException ex)
{
System.out.println("upload fail");
System.out.println(ex);
return false;
}}public static void main(String agrs[]) {
testFtp.connectServer("172.18.114.169",21,"aa","aa");
testFtp.upload(new File("./ftpdir/abcdef.txt"),"aa.txt","./testdir");
testFtp.closeConnect();
}
}问题就是(1)所说的那样,请大家帮忙,代码可直接执行。
import java.io.File;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class testFtp
{
private static FtpClient ftpClient;
public static boolean connectServer(String ip, int port,String user, String password)
{
try
{
ftpClient = new FtpClient();
ftpClient.openServer(ip,port);
ftpClient.login(user,password);
System.out.println("login success!");
return true;
}
catch (IOException ex)
{
System.out.println(ex);
return false;
}
}public static void closeConnect()
{
try
{
ftpClient.closeServer();
}
catch (IOException ex)
{
System.out.println("not disconnect");
System.out.println(ex);
}
}public static boolean upload(File localfilename,String remotefilename,String ftppath)
{
if (ftppath.length() != 0)
{
try
{
ftpClient.cd(ftppath);
/*这是在服务器上建文件夹
String new_dir="20070810";
String cmd_mkdir="MKD "+new_dir+"\r\n;
ftpClient.sendServer(cmd_mkdir);
ftpClient.cd(new_dir);*/
//现在的问题是,如果我不在上传的时候建文件夹,则能上传成功
//一旦建立文件夹,再上传的话,报错:
//sun.net.ftp.FtpProtocolException: PORT :500 'EPRT
//|1|172.18.114.222|4467|': command not understood ---------------(1)
}
catch (IOException ex)
{
System.out.println(ex);
}
}
try
{
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(remotefilename);
File file_in = localfilename;
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
os.write(bytes, 0, c);
is.close();
os.close();
System.out.println("upload success");
return true;
}
catch (IOException ex)
{
System.out.println("upload fail");
System.out.println(ex);
return false;
}}public static void main(String agrs[]) {
testFtp.connectServer("172.18.114.169",21,"aa","aa");
testFtp.upload(new File("./ftpdir/abcdef.txt"),"aa.txt","./testdir");
testFtp.closeConnect();
}
}问题就是(1)所说的那样,请大家帮忙,代码可直接执行。
解决方案 »
- 一个关于构造器初始化的问题 帮我解答下 行么
- 迷宫寻路 java编程 关于stack
- 字符串处理问题,高手指教!!在线等
- 这样一个图片视频上传显示播放功能,三个星期可以实现吗?类似 http://news.sina.com.cn/photo
- 请问如何在写文件时,指定保存文件的字符集,我想让文件保存成UTF-8
- 寻加密算法
- 希望高手来挑战~~!分不是问题(关于窗体应用程序方面的)在线等待
- 谁和我说说,jdk装好后,环境变量怎么设置?
- 关于连接关闭问题
- 请问:如果两个事务共享一个Connection会出现什么问题?
- lucene做成像百度那样的搜索引擎该如何下手???
- 如何在Java IDL中传递null参数?
谢谢大家
/**
* 備份到網絡磁碟機上
*
* @throws Exception
*/
private void backtonet() throws Exception {
try {
Prop prop = Prop.getInstance(oContext_);
NetPath_ = prop.get("CSMADJBAT_FOLDER").trim();
} catch (Exception e) {
throw new Exception("取得 CSMADJBAT_FOLDER 失敗 " + e.getMessage());
} try {
File netdir = new File(NetPath_);
if (!netdir.exists()) {
netdir.mkdirs();
} try {
File file = new File(sUpFilePath);
sUpFileName = file.getName();
FileOutputStream outs = new FileOutputStream(NetPath_ + File.separator + sUpFileName, false);
BufferedOutputStream bouts = new BufferedOutputStream(outs);
FileInputStream ins = new FileInputStream("sUpFilePath");
try {
byte[] buf = new byte[8192];
int readed;
BufferedInputStream bis = new BufferedInputStream(ins);
while ((readed = bis.read(buf)) != -1) {
bouts.write(buf, 0, readed);
}
bouts.flush();
} finally {
outs.close();
outs = null;
ins.close();
ins = null;
}
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
throw new Exception("備份到網絡磁碟失敗 " + ex.getMessage());
}
}
貌似sun的ftpClient没有建立目录的api哦
还是用别的吧...
使用sendserver发送命令后,还应该要解析发回来的返回信息。并不是一个命令发送完后就可以发送下一个命令。有时候要等待到就绪状态才行。
FtpClient中有issueCommand()方法就是完成这个工作的。不过是protected方法。可以直接继承该类后写一个mkd方法。
import java.io.IOException;import sun.net.ftp.FtpClient;
/**
* @author zxum 2007-8-10 下午07:57:39
*
*/
public class MyFtpClient extends FtpClient
{
MyFtpClient(){
super();
}
public void mkd(String dirName){
String cmd = "MKD " + dirName;
try
{
this.issueCommandCheck(cmd);
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args)
{
// TODO 自动生成方法存根 }}
非常感谢windvscloud(大翼)
请问怎么判断ftp服务器上是否存在指定目录?
分不够可以再加
或者你可以先用list()方法取回所有的目录,判断一下就是了撒
drw-rw-rw- 1 user group 0 Aug 10 17:09 .
drw-rw-rw- 1 user group 0 Aug 10 17:09 ..
drw-rw-rw- 1 user group 0 Aug 15 20:05 20070810
顺便问一下,我的程序对有的ftp地址可以成功上传,有的抛异常
501 PORT not allowed after EPSV ALL,你有过这方面的经验吗?