java 下载代码
java从网络下载的简单例子
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; public class DownUtil { public static void download(String urlString, String filename, String savePath) throws Exception { URL url = new URL(urlString); URLConnection con = url.openConnection(); con.setConnectTimeout(60 * 1000); InputStream is = con.getInputStream(); byte[] bs = new byte[1024]; int len; File sf = new File(savePath); if (!sf.exists()) { sf.mkdirs(); } OutputStream os = new FileOutputStream(sf.getPath() + File.separator + filename); while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } os.close(); is.close(); } public static void main(String[] args) { try { download("https://m.seaxiang.com/", "test.txt", "D:\\"); } catch (Exception e) { e.printStackTrace(); } } }
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦