package com.more.mes.tcl;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequests {
/**
* 从网络Url中下载文件
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// conn.setRequestProperty("lfwywxqyh_token",toekn);
//得到输入流
InputStream inputStream = conn.getInputStream();
java通过http方式下载文件
最新推荐文章于 2024-09-13 16:12:01 发布
本文主要介绍了如何在Java中使用HTTP协议进行文件下载操作,通过实例代码详细解析了整个下载过程。
摘要由CSDN通过智能技术生成