我用java做了一个ftp上传文件
public int upload(String dir, String file) throws FileNotFoundException{
FtpConnection con = new FtpConnection("192.168.0.127", 21, "/"); con.addConnectionListener(this);
con.setConnectionHandler(handler);
try{
con.login("admin", "123456");
}catch(Exception ex) { ex.printStackTrace(); }
while(!isThere)
{ System.out.println("FTP登录失败");
try { Thread.sleep(10); }
catch(Exception ex) { ex.printStackTrace(); }
}
//make dirs
String path = "";
String[] paths = StringUtils.split(dir, "/");
for(int i = 0; i < paths.length; i++){
path += "/" + paths[i];
if(!con.chdir(path)){ con.mkdir(path); }
}
String newfile=file.substring(file.lastIndexOf("\\")+1,file.length());
int i=con.upload(file);
con.rename(newfile, upfileFileName);
return i;
} 上传的时候是调用 upload(文件的路径) 方法,我从客户端传了一个地址过来,但是程序总是在服务器上去找那个文件,然后理所当然的就报 文件找不到..的错 请问下 这种问题该怎么解决呢?或者java web 的ftp该怎么做呢 ?
public int upload(String dir, String file) throws FileNotFoundException{
FtpConnection con = new FtpConnection("192.168.0.127", 21, "/"); con.addConnectionListener(this);
con.setConnectionHandler(handler);
try{
con.login("admin", "123456");
}catch(Exception ex) { ex.printStackTrace(); }
while(!isThere)
{ System.out.println("FTP登录失败");
try { Thread.sleep(10); }
catch(Exception ex) { ex.printStackTrace(); }
}
//make dirs
String path = "";
String[] paths = StringUtils.split(dir, "/");
for(int i = 0; i < paths.length; i++){
path += "/" + paths[i];
if(!con.chdir(path)){ con.mkdir(path); }
}
String newfile=file.substring(file.lastIndexOf("\\")+1,file.length());
int i=con.upload(file);
con.rename(newfile, upfileFileName);
return i;
} 上传的时候是调用 upload(文件的路径) 方法,我从客户端传了一个地址过来,但是程序总是在服务器上去找那个文件,然后理所当然的就报 文件找不到..的错 请问下 这种问题该怎么解决呢?或者java web 的ftp该怎么做呢 ?
public static boolean uploadFile(String path, String filename, InputStream input) { FTPClient ftpClient= new FTPClient();
try {
connectServer(ftpClient); ftpClient.changeWorkingDirectory(path);
ftpClient.storeFile(filename, input);
input.close();把文件路径替换成输入流试试看 我就是这样做的
int i=con.upload(文件名,input);是这样的么 ?