【问题标题】:Unable to download the exe file from remote location无法从远程位置下载 exe 文件
【发布时间】:2012-03-18 00:05:46
【问题描述】:

我无法从服务器机器下载 exe 文件。

I.E.我可以从我的位置机器下载一个 exe 文件并将其保存在磁盘中,但不能从其他服务器下载,但是当我尝试从服务器机器访问它时它没有下载,并给出如下错误:

java.io.FileNotFoundException: http:\10.128.10.60\home\test\filexilla.exe(The filename, directory name, or volume label syntax is incorrect)

下面是我的代码:

fileInputStream = new FileInputStream(new File("E:\\Sunnywellshare\\perl\\filezilla.exe"))
//this code is working fine

fileInputStream = new FileInputStream(new File("http://10.127.10.10/test/filezilla.exe"));
//this code is from remote location.and throwing error

如何解决 FileNotFoundException?

【问题讨论】:

    标签: java jakarta-ee


    【解决方案1】:
    import java.net.*;
    import java.io.*;
    
    public class URLReader {
        public static void main(String[] args) throws Exception {
      URL oracle = new URL("http://www.oracle.com/");
      BufferedReader in = new BufferedReader(
            new InputStreamReader(
            oracle.openStream()));
    
      String inputLine;
    
      while ((inputLine = in.readLine()) != null)
          System.out.println(inputLine);
    
      in.close();
        }
    }
    

    【讨论】:

    • 这里的Reader 类会破坏*.exe 文件,它是二进制的,而不是字符数据。您必须直接使用URL 中的InputStream
    • 我认为在你给出答案之前你需要阅读一下:docs.oracle.com/javase/tutorial/networking/urls/readingURL.html
    • 您的代码适用于读取 HTML 文件(例如),但不适用于下载 *.exe 文件,这就是问题所在。如果将此代码用于*.exe 之类的二进制数据,则文件将被损坏。
    • 这是一个更容易获取字节抛出 URL 的问题:stackoverflow.com/questions/2295221/…
    【解决方案2】:

    您无法使用FileInputStream 打开网址!您可以使用URLConnection 并从中获取InputStream;那么您必须从InputStream 复制所有数据并(可能)将其保存到本地文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 2022-10-12
      • 1970-01-01
      相关资源
      最近更新 更多