將遠程文件下載到服務器(轉)

原创
2014/03/04 14:22
阅读数 130
//downFileUrl 遠程文件下載地址
 public void downFile(String downFileUrl) throws Exception {

    try {
     String fileName = downFileUrl.substring(downFileUrl.lastIndexOf("/") + 1); //取文件名
      String savePath = "D:/wwwroot/"+ fileName;  //要另存的路徑+文件名作為路徑
      URL downUrl = new URL(downFileUrl);
     URLConnection conn = downUrl.openConnection();
     int dataSize = conn.getContentLength(); //取得要下載的數據的長度

      BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(savePath)));

      byte[] data = new byte[1024];
      int len = in.read(data);
      while (len != -1) {
        out.write(data, 0, len);
        len = in.read(data);
      }
      in.close();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

  }
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部