Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java代码通过传递URL下载文件_Java - Fatal编程技术网

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

使用java代码通过传递URL下载文件,java,Java,我正在尝试用java编写一个代码,其中用户提供一个url链接,程序获取url链接并按原样下载一个网页并保存在特定位置..与另存为相同。。。在网页上提供选项 有人能帮我吗 提前感谢您可以使用Java URL API获取URL上的输入流,然后从中读取数据,并通过文件上的输出流进行写入 请参见,查看。它有一些功能可以帮助您从网页中提取资源。//示例URL: import java.io.*; 导入java.net。*; 公共类URL下载{ 最终静态整数大小=1024; 公共静态void文件URL(字符

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

有人能帮我吗


提前感谢

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

请参见,

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

//示例URL:

import java.io.*;
导入java.net。*;
公共类URL下载{
最终静态整数大小=1024;
公共静态void文件URL(字符串fAddress、字符串localFileName、字符串destinationDir){
OutputStream outStream=null;
URLConnection=null;
InputStream=null;
试一试{
网址;
字节[]buf;
int字节数,字节写入=0;
url=新url(时尚服饰);
outStream=new BufferedOutputStream(new FileOutputStream(destinationDir+“\\”+localFileName));
uCon=url.openConnection();
is=uCon.getInputStream();
buf=新字节[大小];
而((byteRead=is.read(buf))!=-1){
输出流写入(buf,0,byteRead);
字节写入+=字节数;
}
System.out.println(“下载成功”);
System.out.println(“文件名:\”+localFileName+“\”\n字节数:“+bytewrited”);
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
is.close();
exptream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
公共静态void文件下载(字符串fAddress、字符串destinationDir){
int slashIndex=fAddress.lastIndexOf('/');
int periodIndex=fAddress.lastIndexOf('.');
字符串文件名=fAddress.substring(slashIndex+1);
如果(periodIndex>=1&&slashIndex>=0&&slashIndex

它正在充分发挥作用。

你能说说到目前为止你做了什么吗?您在哪一点被卡住了?请检查您的代码,可能是语句
fileDownload(args[i],args[0])中的args应该被交换。
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 {
        }
    }
}