如何从网站下载文件?
How to download a file from a website?
我正在寻找一种从网站下载文件的方法。我看过这个问题(How to download and save a file from Internet using Java?) already, but I was wondering if you could clear two things up for me. First, let's say the link to the file is "http://www.mediafire.com/download/fasd13z88k7umvm/SecurityCraft+v1.4pre+for+1.6.4.zip”。我会在 URL 构造函数中插入 link:
String url = "http://www.mediafire.com/download/fasd13z88k7umvm/SecurityCraft+v1.4pre+for+1.6.4.zip";
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
但是需要在 FileOutputStream 构造函数中插入什么?第二件事是,fos.getChannel().transferFrom() 将文件保存到哪里?还是我需要执行其他步骤才能将文件保存到我的硬盘?
总之,感谢阅读。
在回答您的第一个问题时,您输入了要保存文件的位置的文件名。 See the docs here.。我认为这回答了你的第二个问题,因为字符串是你想要保存文件的地方。
请记住,如果您使用 相对 路径,文件将保存在应用程序执行的位置,您还需要确保您对该目录具有写入权限。
我正在寻找一种从网站下载文件的方法。我看过这个问题(How to download and save a file from Internet using Java?) already, but I was wondering if you could clear two things up for me. First, let's say the link to the file is "http://www.mediafire.com/download/fasd13z88k7umvm/SecurityCraft+v1.4pre+for+1.6.4.zip”。我会在 URL 构造函数中插入 link:
String url = "http://www.mediafire.com/download/fasd13z88k7umvm/SecurityCraft+v1.4pre+for+1.6.4.zip";
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
但是需要在 FileOutputStream 构造函数中插入什么?第二件事是,fos.getChannel().transferFrom() 将文件保存到哪里?还是我需要执行其他步骤才能将文件保存到我的硬盘?
总之,感谢阅读。
在回答您的第一个问题时,您输入了要保存文件的位置的文件名。 See the docs here.。我认为这回答了你的第二个问题,因为字符串是你想要保存文件的地方。
请记住,如果您使用 相对 路径,文件将保存在应用程序执行的位置,您还需要确保您对该目录具有写入权限。