用java流的方式怎么指定下载到指定目录下
发布网友
发布时间:2022-04-29 17:15
我来回答
共1个回答
热心网友
时间:2023-10-22 15:25
举例代码:
/**
* 下载文件。
* @param urlStr 文件的URL
* @param savePath 保存到的目录
* @param fileName 保存的文件名称
* @param description 描述(如:歌曲,专辑封面,歌词等)
* @throws IOException
*/
public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000); // 设置超时间为10秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 防止屏蔽程序抓取而返回403错误
File saveDir = new File(savePath);
File file = new File(saveDir + File.separator + fileName);
try (InputStream inputStream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
byte[] flowData = readInputStream(inputStream);
fos.write(flowData);
} catch (Exception e) {
MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());
}
MainFrame.logEvent(description + "下载完成:" + url);
}
MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看
热心网友
时间:2023-10-22 15:25
举例代码:
/**
* 下载文件。
* @param urlStr 文件的URL
* @param savePath 保存到的目录
* @param fileName 保存的文件名称
* @param description 描述(如:歌曲,专辑封面,歌词等)
* @throws IOException
*/
public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000); // 设置超时间为10秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 防止屏蔽程序抓取而返回403错误
File saveDir = new File(savePath);
File file = new File(saveDir + File.separator + fileName);
try (InputStream inputStream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
byte[] flowData = readInputStream(inputStream);
fos.write(flowData);
} catch (Exception e) {
MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());
}
MainFrame.logEvent(description + "下载完成:" + url);
}
MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看
热心网友
时间:2023-10-22 15:25
举例代码:
/**
* 下载文件。
* @param urlStr 文件的URL
* @param savePath 保存到的目录
* @param fileName 保存的文件名称
* @param description 描述(如:歌曲,专辑封面,歌词等)
* @throws IOException
*/
public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000); // 设置超时间为10秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 防止屏蔽程序抓取而返回403错误
File saveDir = new File(savePath);
File file = new File(saveDir + File.separator + fileName);
try (InputStream inputStream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
byte[] flowData = readInputStream(inputStream);
fos.write(flowData);
} catch (Exception e) {
MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());
}
MainFrame.logEvent(description + "下载完成:" + url);
}
MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看
热心网友
时间:2023-11-13 16:23
举例代码:
/**
* 下载文件。
* @param urlStr 文件的URL
* @param savePath 保存到的目录
* @param fileName 保存的文件名称
* @param description 描述(如:歌曲,专辑封面,歌词等)
* @throws IOException
*/
public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000); // 设置超时间为10秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 防止屏蔽程序抓取而返回403错误
File saveDir = new File(savePath);
File file = new File(saveDir + File.separator + fileName);
try (InputStream inputStream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
byte[] flowData = readInputStream(inputStream);
fos.write(flowData);
} catch (Exception e) {
MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());
}
MainFrame.logEvent(description + "下载完成:" + url);
}
MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看
热心网友
时间:2023-10-22 15:25
举例代码:
/**
* 下载文件。
* @param urlStr 文件的URL
* @param savePath 保存到的目录
* @param fileName 保存的文件名称
* @param description 描述(如:歌曲,专辑封面,歌词等)
* @throws IOException
*/
public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000); // 设置超时间为10秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 防止屏蔽程序抓取而返回403错误
File saveDir = new File(savePath);
File file = new File(saveDir + File.separator + fileName);
try (InputStream inputStream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
byte[] flowData = readInputStream(inputStream);
fos.write(flowData);
} catch (Exception e) {
MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());
}
MainFrame.logEvent(description + "下载完成:" + url);
}
MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看
热心网友
时间:2023-10-22 15:25
举例代码:
/**
* 下载文件。
* @param urlStr 文件的URL
* @param savePath 保存到的目录
* @param fileName 保存的文件名称
* @param description 描述(如:歌曲,专辑封面,歌词等)
* @throws IOException
*/
public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000); // 设置超时间为10秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 防止屏蔽程序抓取而返回403错误
File saveDir = new File(savePath);
File file = new File(saveDir + File.separator + fileName);
try (InputStream inputStream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
byte[] flowData = readInputStream(inputStream);
fos.write(flowData);
} catch (Exception e) {
MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());
}
MainFrame.logEvent(description + "下载完成:" + url);
}
MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看
热心网友
时间:2023-10-22 15:25
举例代码:
/**
* 下载文件。
* @param urlStr 文件的URL
* @param savePath 保存到的目录
* @param fileName 保存的文件名称
* @param description 描述(如:歌曲,专辑封面,歌词等)
* @throws IOException
*/
public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000); // 设置超时间为10秒
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 防止屏蔽程序抓取而返回403错误
File saveDir = new File(savePath);
File file = new File(saveDir + File.separator + fileName);
try (InputStream inputStream = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file))
{
byte[] flowData = readInputStream(inputStream);
fos.write(flowData);
} catch (Exception e) {
MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());
}
MainFrame.logEvent(description + "下载完成:" + url);
}
MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看