通过使用java代码传递URL来下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2302233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:52:24  来源:igfitidea点击:

Download file by passing URL using java code

java

提问by Bhavika Thakkar

I am trying to write a code in java in which user provide a url link and the program take url link and download a web page as it is and save at particular location..same as save as... option available on webpage.

我正在尝试用 java 编写一个代码,其中用户提供一个 url 链接,程序获取 url 链接并按原样下载网页并保存在特定位置..与网页上提供的另存为...选项相同。

Please can anybody help me

请任何人都可以帮助我

Thanks in advance

提前致谢

回答by Moro

You can use Java URL API to get an input stream on the URL then read the from it and write through output stream on a file.

您可以使用 Java URL API 获取 URL 上的输入流,然后从中读取并通过输出流写入文件。

see read data from url, Write to file

查看从 url 读取数据写入文件

回答by kgiannakakis

Have a look at the HtmlParser. It has some features that will help you extract resources from a web page.

看看HtmlParser。它有一些功能可以帮助您从网页中提取资源。

回答by Nitul

// Sample URL : http://www.novell.com/coolsolutions/tools/downloads/ntradping.zip

// 示例 URL:http: //www.novell.com/coolsolutions/tools/downloads/ntradping.zip

import java.io.*;
import java.net.*;


public class UrlDownload {
    final static int size = 1024;

    public static void fileUrl(String fAddress, String localFileName, String destinationDir) {
        OutputStream outStream = null;
        URLConnection uCon = null;

        InputStream is = null;
        try {
            URL url;
            byte[] buf;
            int byteRead, byteWritten = 0;
            url = new URL(fAddress);
            outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\" + localFileName));

            uCon = url.openConnection();
            is = uCon.getInputStream();
            buf = new byte[size];
            while ((byteRead = is.read(buf)) != -1) {
                outStream.write(buf, 0, byteRead);
                byteWritten += byteRead;
            }
            System.out.println("Downloaded Successfully.");
            System.out.println("File name:\"" + localFileName + "\"\nNo ofbytes :" + byteWritten);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void fileDownload(String fAddress, String destinationDir) {
        int slashIndex = fAddress.lastIndexOf('/');
        int periodIndex = fAddress.lastIndexOf('.');

        String fileName = fAddress.substring(slashIndex + 1);

        if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fAddress.length() - 1) {
            fileUrl(fAddress, fileName, destinationDir);
        } else {
            System.err.println("path or file name.");
        }
    }

    public static void main(String[] args) {
        if (args.length == 2) {
            for (int i = 1; i < args.length; i++) {
                fileDownload(args[i], args[0]);
            }
        } else {
        }
    }
}

It is working fully.

它正在全面工作。