苏北先森为什么入狱:请问:在Java中怎样实现ftp

来源:百度文库 编辑:高校问答 时间:2024/05/02 01:59:55
我是这个方面的菜鸟,请指点

使用ftpClient类

ftpClient的文件路径是相对路径还是绝对路径和Ftp Server的类型有关,我就遇到过wu-ftp是相对路径而vs-ftp是绝对路径的问题,所以你最好在put文件的时候不要用/xxx/xxx这种绝对路径的书写方式。还有ftp用户权限问题,linux下root不能作为ftp用户的所以通常用的用户不一定能够在你的目的路径下有写文件的权限,所以一定要建好目录再传。这个没有什么实际例子,我用的也是网上的例子。
public boolean sendFile(String filepathname, String distfilepathname) {
boolean result = true;
if (aftp != null) {
System.out.println("正在粘贴文件,请耐心等待....");

String contentperline;
try {
a = "粘贴成功!";
String fg = new String("/");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index + 1);
File localFile;
localFile = new File(filepathname);
RandomAccessFile sendFile = new RandomAccessFile(filepathname, "r");
//
sendFile.seek(0);
outs = aftp.put(distfilepathname);
outputs = new DataOutputStream(outs);
while (sendFile.getFilePointer() < sendFile.length()) {
ch = sendFile.read();
outputs.write(ch);
}
outs.close();
sendFile.close();
//FileOperator fop = new FileOperator();
//fop.DeleteFile(filepathname);
}catch (IOException e) {
a = "粘贴失败!";
result = false;
}
System.out.println(a);
//
}else {
result = false;
}
return result;
}