我有通过ftp文件下载的java代码,下载文件后,它转到默认路径。指定的目标路径没有下载的文件。为什么?我的代码是,
public class ftpUpload1
{
public static void main(String a[]) throws IOException
{
ftpUpload1 obj = new ftpUpload1();
URL url1 = new URL("ftp://vbalamurugan:vbalamurugan@192.168.6.38/ddd.txt" );
File dest = new File("D:/rvenkatesan/Software/ddd.txt");
obj.ftpDownload(dest, url1);
public void ftpDownload(File destination,URL url) throws IOException
{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URLConnection urlc = url.openConnection();
bis = new BufferedInputStream( urlc.getInputStream() );
bos = new BufferedOutputStream( new
FileOutputStream(destination.getName() ) );
int i;
//read byte by byte until end of stream
while ((i = bis.read())!= -1)
{
// bos.write(i);
bos.write(i);
}
System.out.println("File Downloaded Successfully");
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
}
下载的文件“ddd.txt”不在“D:/ rvenktesan / Software”中。它位于“D:rvenkatesan / JAVA PROJECTS”。为什么?指导我将文件存储在指定的路径中?非常感谢。
答案 0 :(得分:1)
你的问题是FileOutputStream(destination.getName() ) );
将其更改为:FileOutputStream(destination.getAbsolutePath() ) );
getName将返回文件名" ddd.txt#34;只要。我假设您从D:/rvenkatesan/JAVA PROJECTS