java代码实现 线程池监听队列,启动下载线程,下载数据并保存导文件目录,同时更新mysql相应的请求,填充下载文件字段
时间: 2024-02-28 22:53:40 浏览: 66
java多线程文件下载,将下载任务分片,每片一个线程下载
好的,下面是Java代码实现:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DownloadThreadPool {
private ExecutorService executorService;
private LinkedBlockingQueue<DownloadTask> downloadQueue;
public DownloadThreadPool(int threadPoolSize) {
executorService = Executors.newFixedThreadPool(threadPoolSize);
downloadQueue = new LinkedBlockingQueue<>();
}
public void addDownloadTask(DownloadTask task) {
// 存储到 MySQL 数据库
saveToMysql(task);
// 将任务添加到队列中
downloadQueue.offer(task);
}
public void start() {
// 启动线程池监听队列
executorService.submit(new DownloadRunnable(downloadQueue));
}
private void saveToMysql(DownloadTask task) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
// 执行 SQL
String sql = "UPDATE download SET download_file = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, task.getFilename());
pstmt.setInt(2, task.getId());
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
class DownloadTask {
private int id;
private String url;
private String filename;
public DownloadTask(int id, String url, String filename) {
this.id = id;
this.url = url;
this.filename = filename;
}
public int getId() {
return id;
}
public String getUrl() {
return url;
}
public String getFilename() {
return filename;
}
}
class DownloadRunnable implements Runnable {
private LinkedBlockingQueue<DownloadTask> downloadQueue;
public DownloadRunnable(LinkedBlockingQueue<DownloadTask> downloadQueue) {
this.downloadQueue = downloadQueue;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
DownloadTask task = downloadQueue.take();
// 下载数据并保存到文件目录
downloadAndSave(task);
// 更新 MySQL 数据库
saveToMysql(task);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
private void downloadAndSave(DownloadTask task) {
HttpURLConnection conn = null;
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
// 创建 URL 对象
URL url = new URL(task.getUrl());
// 打开连接
conn = (HttpURLConnection) url.openConnection();
// 设置请求方法
conn.setRequestMethod("GET");
// 设置连接超时时间
conn.setConnectTimeout(5000);
// 设置读取超时时间
conn.setReadTimeout(5000);
// 建立连接
conn.connect();
// 获取输入流
inputStream = conn.getInputStream();
// 创建文件输出流
outputStream = new FileOutputStream(task.getFilename());
// 缓存数组
byte[] buffer = new byte[1024];
// 读取长度
int len;
// 循环读取数据并写入文件
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接和流
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void saveToMysql(DownloadTask task) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
// 执行 SQL
String sql = "UPDATE download SET download_file = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, task.getFilename());
pstmt.setInt(2, task.getId());
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
以上代码实现了一个下载线程池及队列,其中`DownloadTask`表示下载任务,包含任务ID、要下载的URL和文件名;`DownloadRunnable`表示下载线程,从队列中取出任务并执行下载操作,下载完成后更新MySQL数据库;`DownloadThreadPool`表示下载线程池,负责接收用户请求,存储到MySQL数据库,将任务添加到队列中并启动下载线程池监听队列。
阅读全文