请教高人:
     项目遇到:
     FTP 路径: 192.168.0.117  端口21
    user/pwd   sa/sa
    默认FTP 根路径  d:/testFTP
    
    如果直接从根目录下(testFTP) 上传下载文件都没有问题 ,现在是上传文件到该机器的 E:/download 文件夹下,就不知道怎么处理了。 如果用FTP 客户端有个 远程路径 写上 E:/download, 也可以登录到E:/download下。 请问有java如何处理?

解决方案 »

  1.   

    这个上传文件的地址固定么?
    不行的话,你在java中定义变量值保存上传文件的基础地址值
    既然你上传到e盘,那试问你是怎么上传到e盘的呢?
      

  2.   

    就是没实现这个上传到E, 才问的。
        地址固不固定都没关系,地址可以做为参数传递。   FTPclient 没看到有方法实现
      

  3.   

    如果你需要的使用同一个用户sa,需要在服务器端设置alias,即首先共享E:/download给sa,然后给E:/download设置alias比如d:/testFTP/download在filezilla里设置alias必须用完整路径,其他的ftp server不知道这样设置好后(如果d:/testFTP是根目录)你的指向E:/download的 url就是ftp://sa:sa@[ip]/download
      

  4.   

    楼上的意思是:在FTP服务器端进行设置后,访问ftp 的根目录(d:/testFTP),就能操作E:/download 了? java操作ftp不用太特别处理?
      

  5.   

    package nc.ui.doc.doc_007;  
     
    import java.io.File;  
     import java.io.FileInputStream;  
     import java.io.FileOutputStream;  
     import java.io.IOException;  
     
    import nc.itf.doc.DocDelegator;  
     import nc.vo.doc.doc_007.DirVO;  
     import nc.vo.pub.BusinessException;  
     import nc.vo.pub.SuperVO;  
     
    import org.apache.commons.net.ftp.FTPClient;  
     import org.apache.commons.net.ftp.FTPFile;  
     import org.apache.commons.net.ftp.FTPReply;  
     import org.apache.commons.net.ftp.FTP;  
     public class FtpTool {  
     private FTPClient ftp;  
     
    private String romateDir = "";  
     
    private String userName = "";  
     
    private String password = "";  
     
    private String host = "";  
     
    private String port = "21";  
     
    public FtpTool(String url) throws IOException {  
     //String url="ftp://user:password@ip:port/ftptest/psd";  
     int len = url.indexOf("//");  
     String strTemp = url.substring(len + 2);  
     len = strTemp.indexOf(":");  
     userName = strTemp.substring(0, len);  
     strTemp = strTemp.substring(len + 1);  
     
    len = strTemp.indexOf("@");  
     password = strTemp.substring(0, len);  
     strTemp = strTemp.substring(len + 1);  
     host = "";  
     len = strTemp.indexOf(":");  
     if (len < 0)//没有设置端口  
     {  
     port = "21";  
     len = strTemp.indexOf("/");  
     if (len > -1) {  
     host = strTemp.substring(0, len);  
     strTemp = strTemp.substring(len + 1);  
     } else {  
     strTemp = "";  
     }  
     } else {  
     host = strTemp.substring(0, len);  
     strTemp = strTemp.substring(len + 1);  
     len = strTemp.indexOf("/");  
     if (len > -1) {  
     port = strTemp.substring(0, len);  
     strTemp = strTemp.substring(len + 1);  
     } else {  
     port = "21";  
     strTemp = "";  
     }  
     }  
     romateDir = strTemp;  
     ftp = new FTPClient();  
     ftp.connect(host, FormatStringToInt(port));  
     
    }  
     
    public FtpTool(String host, int port) throws IOException {  
     ftp = new FTPClient();  
     ftp.connect(host, port);  
     }  
     
    public String login(String username, String password) throws IOException {  
     this.ftp.login(username, password);  
     return this.ftp.getReplyString();  
     }  
     
    public String login() throws IOException {  
     this.ftp.login(userName, password);  
     System.out.println("ftp用户: " + userName);  
     System.out.println("ftp密码: " + password);  
     if (!romateDir.equals(""))  
     System.out.println("cd " + romateDir);  
     ftp.changeWorkingDirectory(romateDir);  
     return this.ftp.getReplyString();  
     }  
     
    public boolean upload(String pathname, String filename) throws IOException, BusinessException {  
     
    int reply;  
     int j;  
     String m_sfilename = null;  
     filename = CheckNullString(filename);  
     if (filename.equals(""))  
     return false;  
     reply = ftp.getReplyCode();  
     if (!FTPReply.isPositiveCompletion(reply)) {  
     ftp.disconnect();  
     System.out.println("FTP server refused connection.");  
     System.exit(1);  
     }  
     FileInputStream is = null;  
     try {  
     File file_in;  
     if (pathname.endsWith(File.separator)) {  
     file_in = new File(pathname + filename);  
     } else {  
     file_in = new File(pathname + File.separator + filename);  
     }  
     if (file_in.length() == 0) {  
     System.out.println("上传文件为空!");  
     return false;  
     }   
       //产生随机数最大到99   
       j = (int)(Math.random()*100);  
       m_sfilename = String.valueOf(j) + ".pdf"; // 生成的文件名  
     is = new FileInputStream(file_in);  
     ftp.setFileType(FTP.BINARY_FILE_TYPE);  
     ftp.storeFile(m_sfilename, is);  
     ftp.logout();  
     } finally {  
     if (is != null) {  
     is.close();  
     }  
     }  
     System.out.println("上传文件成功!");  
     return true;  
     }  
     public boolean delete(String filename) throws IOException {  
     
    FileInputStream is = null;  
     boolean retValue = false;  
     try {  
     retValue = ftp.deleteFile(filename);  
     ftp.logout();  
     } finally {  
     if (is != null) {  
     is.close();  
     }  
     }  
     return retValue;  
     
    }  
     
    public void close() {  
     if (ftp.isConnected()) {  
     try {  
     ftp.disconnect();  
     } catch (IOException e) {  
     e.printStackTrace();  
     }  
     }  
     }  
     
    public static int FormatStringToInt(String p_String) {  
     int intRe = 0;  
     if (p_String != null) {  
     if (!p_String.trim().equals("")) {  
     try {  
     intRe = Integer.parseInt(p_String);  
     } catch (Exception ex) {  
     
    }  
     }  
     }  
     return intRe;  
     }  
     
    public static String CheckNullString(String p_String) {  
     if (p_String == null)  
     return "";  
     else  
     return p_String;  
     }  
     
    public boolean downfile(String pathname, String filename) {  
     
    String outputFileName = null;  
     boolean retValue = false;  
     try {  
     FTPFile files[] = ftp.listFiles();  
     int reply = ftp.getReplyCode();  
     
    ////////////////////////////////////////////////  
     if (!FTPReply.isPositiveCompletion(reply)) {  
     try {  
     throw new Exception("Unable to get list of files to dowload.");  
     } catch (Exception e) {  
     // TODO Auto-generated catch block  
     e.printStackTrace();  
     }  
     }  
     
    /////////////////////////////////////////////////////  
     if (files.length == 0) {  
     System.out.println("No files are available for download.");  
     }else {  
     for (int i=0; i <files.length; i++) {  
     System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());  
     outputFileName = pathname + filename + ".pdf";  
     //return outputFileName;  
     File f = new File(outputFileName);  
     
    //////////////////////////////////////////////////////  
     retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f));  
     
    if (!retValue) {  
     try {  
     throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");  
     } catch (Exception e) {  
     // TODO Auto-generated catch block  
     e.printStackTrace();  
     }  
     }  
     
    }  
     }  
     
    /////////////////////////////////////////////////////////////  
     } catch (IOException e) {  
     // TODO Auto-generated catch block  
     e.printStackTrace();  
     }  
     return retValue;  
     }  
     
    }  
     试试这个 
      

  6.   

    谢谢楼上//String url="ftp://user:password@ip:port/ftptest/psd";  没看明白
    怎么知道是访问的那个盘符下的文件夹?
      

  7.   

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    FileInputStream input =new FileInputStream(new File("D:/test.txt"));     
    FTPClient ftp=new FTPClient();
    ftp.connect("192.168.0.117");
    ftp.login("sa", "sa");  
    int  reply = ftp.getReplyCode();    
    ftp.changeWorkingDirectory("e:/FtpTest");    
    ftp.storeFile("test.txt", input);      
          input.close();      
                ftp.logout();      
    }catch(Exception e){   
    e.printStackTrace();
    }   
    }这样是否也可以?
      

  8.   

    ftp 只能上传到 那台机器开启ftp服务的路径下,   不能是上传机器来确定要上传到哪里。
    要执行上面的文件或者把 ftp服务器指定的文件目录下执行文件 或者拷贝一份到任意路径(可以是E/download)的话 要使用telnet 连接该机器
      

  9.   


    你说java操作是指java来做客户端访问你的ftp服务器么?
    如果是,建立alias之后e:\download被ftp映射成了根目录下的一个子目录所以当你访问/download时就会进入你的e:\download这和设置http的alias很相似不知道你试了没有
      

  10.   

    刚才没看到,这样写是不行的,想像你d:\FtpTest被设成了sa这个用户的根目录,如果将e:\download映射成/download后,你的这行应该是ftp.changeWorkingDirectory("/download");
    ftp客户端是不能得不到盘符的
    ps:话说楼主在法国? 
      

  11.   

    这样是不可以的,只能是FTP指定目录的子目录,要是想访问哪里就访问哪里,这个路径就没有意义了
      

  12.   

     
    ++1
    所以要到E盘去 可以用其他技术实现telent