整个社区的早上好。我告诉你一点; 我遇到了一个小应用程序的问题,希望让我从服务器到我的计算机进行自动备份。 (我知道有类似的APPS可以做到,但我想自己做)。我得到的错误主要是:
java.io.FileNotFoundException: I:\FTP 23-04-2017 (Acceso denegado)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at com.cristianvalero.ftp.download.FTPDownload.downloadFile(FTPDownload.java:91)
at tester.main(tester.java:39)
主要测试员类:
public class tester
{
public static void main(String[] args)
{
FTPDownload ftpDownload = new FTPDownload("127.0.0.1", 21, "root", "123");
ftpDownload.conect();
if (ftpDownload.isConnected())
{
ftpDownload.downloadFile("eula.txt", null);
}
}
}
FTPDownload类:
public class FTPDownload
{
private String server;
private int port;
private String user;
private String passwd;
private FTPClient ftpClient;
public FTPDownload(String server, int port, String user, String passwd)
{
this.server = server;
this.port = port;
this.user = user;
this.passwd = passwd;
ftpClient = new FTPClient();
}
public void conect()
{
try
{
ftpClient.connect(this.server, this.port);
ftpClient.login(this.user, this.passwd);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println("[FTP] Successfully connected.");
}
catch (IOException e)
{
catchPrinting(e);
}
}
public boolean isConnected()
{
return ftpClient.isConnected();
}
public boolean downloadFile(String remotePath, String optionalPathSaveDownloaded)
{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
boolean success = false;
if (optionalPathSaveDownloaded == null)
{
System.out.println("[FTP] Setting default saved files on desktop because downlaod path is null...");
optionalPathSaveDownloaded = "I:/FTP "+dtf.format(localDate)+"/";
createFolder(optionalPathSaveDownloaded);
//optionalPathSaveDownloaded = "C://Users//"+System.getProperty("user.name")+"//Desktop//FTP//";
}
try
{
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(optionalPathSaveDownloaded));
success = ftpClient.retrieveFile(remotePath, outputStream1);
outputStream1.close();
if (success)
{
System.out.println("[FTP] File downlaoded successfully.");
}
}
catch (FileNotFoundException e)
{
catchPrinting(e);
}
catch (IOException e)
{
catchPrinting(e);
}
finally
{
try
{
if (ftpClient.isConnected())
{
ftpClient.logout();
ftpClient.disconnect();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return success;
}
private void catchPrinting(Exception e)
{
System.out.println("-----------------------------------------------------");
System.out.println("[FTP] ¡OPS! An internal error has ocurred.");
e.printStackTrace();
System.out.println("-----------------------------------------------------");
}
private void createFolder(String folderPath)
{
File f = new File(folderPath);
if (!f.exists())
{
f.mkdir();
}
}
}
驱动器&#34; I:&#34;我只是一个与磁盘完全不同的分区,而不是&#34; C:&#34;
答案 0 :(得分:0)
您提供给FileOutputStream
构造函数的路径必须是要写入的文件的路径,而不是文件夹的路径。它如何知道要创建的文件的名称?
optionalPathSaveDownloaded = "I:/FTP "+dtf.format(localDate)+"/"+"somefilename.ext";