java ftp中的setPermission

时间:2016-11-15 23:35:44

标签: java

我正在尝试在FTP服务器上创建一个目录并上传该目录中的文件。目录已成功创建但文件未上载可能是由于写入权限

FTPClient ftpClient = new FTPClient();
public void ftpConnection(){


    try {
       ftpClient.connect(server,port);
       ftpClient.enterLocalPassiveMode();
       int replyCode = ftpClient.getReplyCode();
       if (!FTPReply.isPositiveCompletion(replyCode)) {
       System.out.println("Operation failed. Server reply code: " + replyCode);
       }
       boolean success = ftpClient.login(user, pass);
       if (!success) {
           System.out.println("Could not login to the server");
        } else {
              System.out.println("LOGGED IN SERVER");
              boolean created = ftpClient.makeDirectory("/usr/prtsim");
              int returnCode = ftpClient.getReplyCode();
              System.out.println(returnCode);
              if(created){
                 System.out.println("created");
                 ftpClient.changeWorkingDirectory("/usr/prtsim");
                 uploadFile();
              }
              else{
                  if (returnCode == 550) 
                      System.out.println("Directory already present");
                  else
                      System.out.println("not created");
              }

            ftpClient.logout();
            ftpClient.disconnect();
        } 
    }
    catch(IOException ex) {
            System.out.println("Oops! Something wrong happened");
            ex.printStackTrace();
    }
}  

public void uploadFile() throws FileNotFoundException, IOException{


    File firstLocalFile = new     File("C:\\Users\\sswaroo\\Desktop\\sampletext.txt");
    //ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    String firstRemoteFile = "sampletext.txt";
    InputStream inputStream = new FileInputStream(firstLocalFile);
    System.out.println("Start uploading first file");
    boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
    inputStream.close();
    if (done) {
               System.out.println("The first file is uploaded successfully.");
             }

    }

}

我收到了以下回复:

LOGGED IN SERVER
257
created
Start uploading first file
The first file is uploaded successfully.

你能帮我设置我创建的目录的写权限吗?

1 个答案:

答案 0 :(得分:0)

根据您的输出,您似乎永远不会到达

System.out.println("Start uploading first file");

电话

new FileInputStream(firstLocalFile) 
如果无法访问传递给它的文件,

将抛出FileNotFoundException

我建议您检查方法开头创建的文件是否正确且可访问。