java url 下载

 /*
  * 循环下载附件
  */
 public static String insertDownlo(String name, String urls) {
  System.out.println("--name---" + name);
  String isurl ="E:/excels/" + name;
  isurl = isurl.replaceAll("\\\\", "/");
  File file = null;
  try {
   //String downloadURL = "http://10.27.2.61:8082/" + urls;
   System.out.println("downloadURL----" + urls);
   URL url = new URL(urls.replaceAll("\\\\", "/"));
   URLConnection connection = url.openConnection();
   InputStream is = connection.getInputStream();
   
   file = new File(isurl);
   OutputStream os = new FileOutputStream(file);
   int bytesRead = 0;
   byte[] buffer = new byte[8192];
   while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
    os.write(buffer, 0, bytesRead);
   }
   os.close();
   is.close();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

  return file.getAbsolutePath();
 }
 

你可能感兴趣的:(java url 下载)