|
java file copy
hello,
I am trying to copy files from network to local disk. I have this working using URL and inputStream. my problem is that I want to throw exception for both connection failure and filenotfound . I tested with different scenerios and I could not get filenotfound exception. it keeps saying that connection error , here is a breif list of what i am doing
-get file using url
-check if the connection fail, if fail return error message
-if connection is good, proceed and get file
-if file does not exist on the source machine, throw filenotfound and return error message
-if every goes well, copy over the file to localdisk.
here is partial code
...
host = "file:\\\\sourceMachine\\content\\BIGfile.big" ;
URL url = new URL(host);
URLConnection urlC = url.openConnection();
// Copy resource to local file
InputStream is = url.openStream();
System.out.flush();
FileOutputStream fos = null;
String localFile = null;
// Get only file name
StringTokenizer st = new StringTokenizer(url.getFile(), "/");
while (st.hasMoreTokens()) {
localFile = st.nextToken();
}
fos = new FileOutputStream(localFile);
byte[] buf = new byte[32768];
int len;
while ((len = is.read(buf)) > 0) {
fos.write(buf, 0, len);
}
is.close();
fos.close();
} catch (IOException e) {
e.printStackTrace()
}
everytime, i change the invalid machine name, it gives me unknownhost error(which is correct), but when I tried to change invalid filename it gives me conenction error (java.net.ConnectException: Connection refused: connect) but this is not what i want, i want file not found exception. anyone help me on this. thanks.
Cheers
|