如何下载文件并在本地获取路径位置 [英] How to download a file and get the path location locally

查看:346
本文介绍了如何下载文件并在本地获取路径位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址,即 http://downloadplugins.verify.com/Windows/SubAngle.exe
如果我将其粘贴到选项卡上并按Enter键,则文件(SubAngle.exe)将被下载并保存在下载文件夹中。这是手动过程。但可以使用java代码完成。
我在文件名的帮助下编写了获取绝对路径的代码,即SubAngle.exe。

I have a URL i.e http://downloadplugins.verify.com/Windows/SubAngle.exe . If i paste it on the tab and press enter then the file (SubAngle.exe) is getting downloaded and saved in the download folder.This is manual process.But it can be done with java code. I wrote the code for getting the absolute path with the help of file name i.e SubAngle.exe.

要求: - 借助URL文件获取已下载,验证文件已下载并返回文件的绝对路径。

Requirement:- With the help of the URL file gets downloaded,Verify the file has been downloaded and returns the absolute path of the file.

where  locfile is "http://downloadplugins.verify.com/Windows/SubAngle.exe"  

  public String downloadAndVerifyFile(String locfile) {
     File fileLocation = new File(locfile); 
     File fileLocation1 = new File(fileLocation.getName());
     String fileLocationPath = null;
     if(fileLocation.exists()){
      fileLocationPath = fileLocation1.getAbsolutePath();

     }
     else{
         throw new FileNotFoundException("File with name "+locFile+" may not exits at the location");
     }
     return fileLocationPath;
}


推荐答案

而不是写这个巨大的代码,转到 Apache的commons.io
试试这个:

Instead of writing this huge code, go for Apache's commons.io Try this:

URL ipURL = new URL("inputURL");
File opFile = new File("outputFile");
FileUtils.copyURLToFile(ipURL, opFile);

这篇关于如何下载文件并在本地获取路径位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆