日期:2014-05-20  浏览次数:20800 次

java 下载图片问题
  public static void main(String[] args)throws IOException {
 String targetImage = "http://www.baidu.com/img/baidu_sylogo1.gif";  
 URL url = null;  
 FileOutputStream outfile = null;  
 InputStream infile = null;  
 URLConnection con = null;  
      File file=new File("E:/myeclispeWorkbase/samplev/WebRoot/downloadImg");
 System.out.println(file);
     byte [] buffer = null;  
try{
  url = new URL(targetImage);  
  outfile = new FileOutputStream(file);  
  con = url.openConnection();  
  infile = con.getInputStream();  
  buffer = new byte[1024]; 
  while(infile.read(buffer, 0, 1024) != -1){  
  outfile.write(buffer);  
            }  
}catch (Exception e) {
e.printStackTrace();
}finally{
infile.close();
outfile.close();
}

}
麻烦帮我看看这段代码有什么问题,为什么一运行就告诉我
E:/myeclispeWorkbase/samplev/WebRoot/downloadImg 访问被拒绝
我的目的很简单 就是要从网络上获取一张图片,然后保存在项目的某个文件夹中
------解决方案--------------------
没人回答啊?最近回答下 。原因是:file是个文件夹,文件夹不能有写的功能,应该这样:
File file = new File("/downloadImg/a.jpg");
 if (!file.exists()) file.createNewFile(); 如果这个(a.jpg)文件不存在就创建