java 获取网络图片
String photoUrl = data.getString("attachment"); String filename = photoUrl.substring(photoUrl.lastIndexOf("/")); //生成图片链接的url类 URL urlImg = new URL(photoUrl); HttpURLConnection httpcon =(HttpURLConnection)urlImg.openConnection(); httpcon.addRequestProperty("User-Agent","Mozilla / 4.76"); InputStream is = httpcon.getInputStream(); //定义字节数组大小 byte[] buffer = new byte[1024]; ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); int rc = 0; while ((rc = is.read(buffer, 0, 100)) > 0) { swapStream.write(buffer, 0, rc); } buffer = swapStream.toByteArray(); String unit_id=getCommont().getUnit_id(); String destFilePath=Constant.ANNEXPATH+File.separator+getCommon().getOrgan_code()+File.separator+unit_id+File.separator+"JX"; File file=new File(destFilePath); /** *文件路径是否存在 不存在则重新创建 配置路径/区划代码/单位ID/业务类型 */ if(!file.exists()){ file.mkdirs(); } file=null; /** * 文件命名规则:上传时间_附件设置ID */ String attach_name=DateTime.toLocaleFileString()+"_"+annexSet.getAnnexSetId()+"."+FileUtil.getExtension(filename); File fj=new File(destFilePath,attach_name); if (!fj.exists()){ fj.createNewFile(); } try (OutputStream outputStream = new FileOutputStream(fj);) { outputStream.write(buffer); }