~纯纯欲动~ 2022-07-01 18:21 采纳率: 0%
浏览 43
已结题

能够清楚的解释下每句代码的意思

public static void getInternetResource(String savepath, String resurl, String fileName) throws MalformedURLException {
    URL url = null;
    HttpURLConnection con = null;
    InputStream in = null;
    FileOutputStream out = null;
    try {
        url = new URL(resurl);//建立http连接,得到连接对象
        con = (HttpURLConnection) url.openConnection();
        in = con.getInputStream();//得到输入流
        length1 = (int) con.getContentLength();//这个就是下载的文件(不单指文件)大小
        System.out.println("文件长度为" + length1);

        System.out.println(savepath);
        file = new File(savepath + "//" + fileName);//得到文件    
        //System.out.println(it2.getText()+"//"+it3.getText());
        out = new FileOutputStream(file);//得到文件输出流

        int b = -1;
        while ((b = in.read()) != -1) {
            out.write(b);
            double d = (100 * file.length() / length1);//System.out.println(file.length());   
            myBar.setValue((int) d);//刷新进度条        
        }
        JOptionPane.showMessageDialog(frame, "文件下载完毕!", "恭喜!", JOptionPane.ERROR_MESSAGE);
        in.close();
        out.close();
        System.out.println("下载完毕!");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != out) {
                out.close();
            }
            if (null != in) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println("下载函数测试完成!!!");
}
  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2022-07-01 19:07
    关注

    代码中不是有注释吗。
    这个方法是获取网络文件内容,将内容写入savepath + "//" + fileName文件中。

    评论 编辑记录

报告相同问题?

问题事件

  • 专家修改了标签 7月4日
  • 已结题 (查看结题原因) 7月1日
  • 创建了问题 7月1日