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 42 43 44 45 46
|
public static String downloadImgByURL(String urlPath,String savePath,String filename) throws Exception { URL url = new URL(urlPath); URLConnection con = url.openConnection(); con.setConnectTimeout(3*1000); InputStream is = con.getInputStream(); byte[] bs = new byte[1024]; int len; File sf=new File(savePath); if(!sf.exists()){ sf.mkdirs(); } int randomNo=(int)(Math.random()*1000000); if(filename == null || "".equals(filename)){ filename = new java.text.SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+randomNo+".jpg"; } String fullname = sf.getPath()+"\\"+ filename ; OutputStream os = new FileOutputStream(fullname); String virtualPath= sf.getPath() + "\\" +filename; while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } os.close(); is.close(); return fullname; }
|