使用url字符串下载文件
import java.net.URL;
其实都是靠这个包吧,该有的方法都封装好了,到时候研究一下
这个拿来就用
public static void download(String xString) throws IOException { long beginData = System.currentTimeMillis(); URL partUrl = new URL(xString); HttpURLConnection urlConn = (HttpURLConnection) partUrl.openConnection(); urlConn.setReadTimeout(10000 * 10000); urlConn.setConnectTimeout(10000 * 10000); String nameString = xString.substring(xString.lastIndexOf("/") + 1, xString.length()); //指定文件下载目录 // String path = xString.replace("https://repo1.maven.org/maven2/", "D:\\org\\"); // String pathFile = path.replace(nameString, "").replaceAll("\\\\", "//"); // new File("E:/00WYY/task3grabInformation/download").mkdirs(); File file = new File("E:/00WYY/task3grabInformation/download/"+nameString); InputStream is = urlConn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); System.out.println("开始下载" + xString); int len = 0; while ((len = bis.read()) != -1) { bos.write(len); } System.out.println("下载耗时 "+(System.currentTimeMillis()-beginData)+"毫秒"); bis.close(); bos.close(); }
这个博客还没看,感觉有用
Java IO教程 --- Java 下载URL到文件中 (hicxy.com)
浙公网安备 33010602011771号