1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| /** * 根据URL地址获取文件 * @param path URL网络地址 * @return File */ private static File getFileByHttpURL(String path){ String newUrl = path.split("[?]")[0];//去掉参数 String[] suffix = newUrl.split("/"); //得到最后一个分隔符后的名字 String fileName = suffix[suffix.length - 1]; File file = null; InputStream inputStream = null; OutputStream outputStream = null; try{ file = File.createTempFile("report",fileName);//创建临时文件 URL urlFile = new URL(newUrl); inputStream = urlFile.openStream(); outputStream = new FileOutputStream(file);
int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead=inputStream.read(buffer,0,8192))!=-1) { outputStream.write(buffer, 0, bytesRead); } }catch (Exception e) { e.printStackTrace(); }finally { try { if (null != outputStream) { outputStream.close(); } if (null != inputStream) { inputStream.close(); }
} catch (Exception e) { e.printStackTrace(); } } return file; }
|