繁体   English   中英

错误而从下载网址文件

[英]Error while downloading a file from url

我正在尝试从url读取文件并使其成为File类型。

下面是代码

public File fileFromUrl(String str) throws IOException
      {
          File file = new File ("image.png");
          URL url = new URL (str);
            InputStream input = url.openConnection().getInputStream();
            try {

                OutputStream output = new FileOutputStream (file);
                try {
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                        output.write(buffer, 0, bytesRead);
                    }
                } finally {
                    output.close();
                }
            } finally {
                input.close();
            }
          return file;
      }

但是我在OutputStream output = new FileOutputStream (file);时遇到错误OutputStream output = new FileOutputStream (file);

请告诉我如何制作我的url File

如果发布了您遇到的异常,这将很有帮助,但是我猜测您没有尝试在其中创建输出文件的当前工作目录的写权限。

如果要查看它尝试在哪里写入文件,请将此诊断添加到程序中:

System.out.println(
  "Absolute path of image.png: <" + file.getAbsolutePath( ) + ">"
); 

精细。 而不是File file = new File ("image.png"); 使用文件的绝对路径。 File file = new File ("<absolute-path-from-root-directory>");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM