gpt4 book ai didi

java - 下载/保存可直接从给定 URL 获取的文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:22:05 25 4
gpt4 key购买 nike

我有类似 this link 的链接,直接询问保存的文件名,然后在浏览器中开始下载。

如何以编程方式下载或保存此文件?

我尝试了以下方法:

static void DownloadFile(String url, String fileName) throws MalformedURLException, IOException
{
url = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=DESCRIBE+<"+ url +">&format=text%2Fcsv";
URL link = new URL(url); //The file that you want to download

InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
System.out.println("Finished");
}

但这会将仅第一行的文件保存为““主语”,“谓语”,“宾语”“而不是完整的文件。

编辑:

按照答案中的建议,我尝试了以下操作,但这也只给出了文件的第一行:

static void DownloadFile(String s_url, String fileName) throws MalformedURLException, IOException
{
s_url = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=DESCRIBE+<"+ s_url +">&format=text%2Fcsv";
//url = "http://dbpedia.org/data/Sachin_Tendulkar.rdf";
try {
URL url = new URL(s_url); //The file that you want to download

// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
PrintWriter out = new PrintWriter(fileName);

String line;
while ((line = in.readLine()) != null) {
out.println(line);
}
in.close();
out.close();
}
catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
System.out.println("Finished");
}

编辑:

我也尝试过使用 Apache FileUtils,但这也只给出了文件的第一行。

static void DownloadFile(String s_url, String fileName) throws MalformedURLException, IOException
{
s_url = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=DESCRIBE+<"+ s_url +">&format=text%2Fcsv";
URL url = new URL(s_url); //The file that you want to download
FileUtils.copyURLToFile(url, new File(fileName));
System.out.println("Finished");
}

最佳答案

如果您想下载文件,Apache Commons 正是您想要的东西,效果非常好!

org.apache.commons.io.FileUtils.copyURLToFile(new URL("URL")), new File("path/to/file"));

如果 url 返回文本,您可以尝试如下操作:

 try {   
URL url = new URL("http://www.google.com:80/");

// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
PrintWriter out = new PrintWriter("filename.txt");

String line;
while ((line = in.readLine()) != null) {
out.println(line);
}
in.close();
out.close();
}
catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}

关于java - 下载/保存可直接从给定 URL 获取的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27077759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com