无法从远程位置下载exe文件
问题描述:
我无法从服务器机器下载exe文件。无法从远程位置下载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异常?
答
您无法使用FileInputStream
打开网址!您可以使用URLConnection
并从中获得InputStream
;那么你必须复制InputStream
的所有数据,并且(大概)将其保存到本地文件中。
答
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'。 – 2012-02-28 14:21:24
我想你需要阅读一点之前,你给你的答案:http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html – Churk 2012-02-28 14:22:15
你的代码适合阅读一个HTML文件(例如, ),但不能下载一个'* .exe'文件,这就是问题所在。如果您使用此代码来处理二进制数据(如* .exe),则该文件将被损坏。 – 2012-02-28 14:47:59