下载远程服务器单个文件方法

转载自博客:https://www.cnblogs.com/chafe/p/6408516.html

public
static void downloadFile(String remoteFilePath, String localFilePath){ URL urlfile = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File f = new File(localFilePath); try { urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection)urlfile.openConnection(); httpUrl.connect(); bis = new BufferedInputStream(httpUrl.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(f)); int len = 2048; byte[] b = new byte[len]; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } System.out.println("上传成功"); bos.flush(); bis.close(); httpUrl.disconnect(); } catch (Exception e) { e.printStackTrace(); } finally { try { bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } } // public static void main(String[] args) { // String remoteFilePath="http://172.18.49.196:9088/easkLogAnalyse/upload/IOS_AUDIT_LOG_20170207115053_18143481737.log"; // String localFilePath="F:/createFile/createFile/IOS_AUDIT_LOG_20170207115053_18143481737.log"; // downloadFile(remoteFilePath,localFilePath); // }

猜你喜欢

转载自www.cnblogs.com/mangwusuozhi/p/11127614.html
今日推荐