AM尝试使用此代码在java中使用url&urlconnection类从网上下载文件 .
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(destination);
} catch (FileNotFoundException ex) {
}
try {
URL url = null;
try {
url = new URL("here is the url");
//
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
URLConnection urlc = null;
try {
urlc = url.openConnection();
System.out.println("Conneceted");
} catch (IOException ex) {
ex.printStackTrace();
}
try {
bis = new BufferedInputStream(urlc.getInputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
bos = new BufferedOutputStream(fos);
// System.out.println("absolute path="+destination.getAbsolutePath());
int i;
try {
while ((i = bis.read()) != -1) {
System.out.print((char) i);
bos.write(i);
}
JOptionPane.showMessageDialog(this, "downloadeded sucessfully");
this.dispose();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "could not be diownloaded \n please try again");
ex.printStackTrace();
}
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
当我在我的Windows系统上运行此代码时,它运行得很完美但是当我把我的代码带到mac os时,它给了我一个例外
Exception in thread "Thread-2" java.lang.NullPointerException
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at java.io.FilterOutputStream.close(FilterOutputStream.java:140)
at FileDownloader.Download(FileDownloader.java:201)
at FileDownloader$2.run(FileDownloader.java:114)
at java.lang.Thread.run(Thread.java:637)
谁能告诉我原因是什么?以及我如何解决这个问题?
提前致谢