【问题标题】:Java Desktop file download [closed]Java桌面文件下载[关闭]
【发布时间】:2014-07-21 13:49:26
【问题描述】:

在制作桌面应用程序时,我希望它能够从服务器下载视频文件并将其保存在客户端的 PC 中。 怎么做?我不知道。 使用什么工具?请指导 我期待某种集成的 FTP 之类的东西。我不清楚它是如何工作的。

【问题讨论】:

  • 最简单的方法可能是URLConnection

标签: java desktop-application netbeans-platform


【解决方案1】:

您想在哪里下载文件?直接网址或youtube之类的?

类似的东西:

public class DownloadToFile
{
    private URL from;
    private String to;

    public DownloadToFile(URL url, String path_to)
    {
        this.URL = url;
        this.to = path_to;
    }

    public DownloadToFile(String url, String path_to)
    {
        try
        {
            this.URL = new URL(url);
        }
        catch(MalformedURLException e)
        {
            e.printStackTrace();
        }

        this.to = path_to;
    }

    public void saveFile() throws IOException 
    {
        BufferedInputStream in = new BufferedInputStream(this.from.openStream());
        FileOutputStream out = new FileOutputStream(this.to);
            try 
            {
                final byte data[] = new byte[1024];
                    int count;
                        while ((count = in.read(data, 0, 1024)) != -1)
                        {
                            out.write(data, 0, count);
                        }
            }
            finally
            {
                if (in != null)
                {
                    in.close();
                }
        if (fout != null)
        {
            fout.close();
        }
    }
}

【讨论】:

猜你喜欢
  • 2020-07-06
  • 2022-11-15
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 2010-09-08
相关资源
最近更新 更多