远程下载文件
默认信任远程核心服务
/** * 远程下载文件并读取返回 * @param filePath 文件网络地址 http://www.baidu.com/1.txt * @return String */ public String DownAndReadFile(String filePath){ String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); File savePath = new File("D://"+date);//创建新文件 if (!savePath.exists()) { savePath.mkdir(); } String[] urlname = filePath.split("/"); int len = urlname.length-1; String uname = urlname[len];//获取文件名 System.out.println("uname:"+uname); try { File file = new File(savePath+"//"+uname);//创建新文件 if(file!=null && !file.exists()){ file.createNewFile(); } OutputStream oputstream = new FileOutputStream(file); URL url = new URL(filePath); HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); uc.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true uc.connect(); InputStream iputstream = uc.getInputStream(); System.out.println("file size is:"+uc.getContentLength());//打印文件长度 byte[] buffer = new byte[4*1024]; int byteRead = -1; while((byteRead=(iputstream.read(buffer)))!= -1){ oputstream.write(buffer, 0, byteRead); } oputstream.flush(); iputstream.close(); oputstream.close(); //读取文件 StringBuffer strb = new StringBuffer(); FileInputStream fs = new FileInputStream(new File(savePath+"//"+uname)); InputStreamReader isr = new InputStreamReader(fs,"UTF-8"); BufferedReader br = new BufferedReader(isr); String data = ""; while((data = br.readLine()) != null){ strb.append(data + "\n"); } br.close(); fs.close(); isr.close(); System.out.println(strb.toString()); return strb.toString(); } catch (Exception e) { System.out.println("读取失败!"); e.printStackTrace(); } return null; }