upload功能
Change-Id: Iad725ce3e2edd913531bf11705bf51000dde010d
diff --git a/src/main/java/com/pt5/pthouduan/config/TorrentClientMain.java b/src/main/java/com/pt5/pthouduan/config/TorrentClientMain.java
new file mode 100644
index 0000000..08216f0
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/config/TorrentClientMain.java
@@ -0,0 +1,35 @@
+package com.pt5.pthouduan.config;
+
+import com.turn.ttorrent.client.SimpleClient;
+
+import java.net.InetAddress;
+
+public class TorrentClientMain {
+
+ public static void main(String[] args) {
+ try {
+ // 1. 创建客户端对象
+ SimpleClient client = new SimpleClient();
+
+ // 2. 获取本地监听 IP(注意不能用 127.0.0.1,如果其他 peer 不在同一机器)
+ InetAddress address = InetAddress.getLocalHost();
+
+ // 3. 开始下载
+ client.downloadTorrent(
+ "D:\\torrenttest\\doenload\\video.mp4.torrent", // torrent 文件路径
+ "D://torrenttest/111111", // 下载输出目录
+ address // 本机地址
+ );
+
+ System.out.println("Download completed.");
+
+ // 4. 如果不想继续做 Seeder,可以停止客户端
+ client.stop();
+
+ } catch (Exception e) {
+ System.err.println("Download failed:");
+ e.printStackTrace();
+ }
+ }
+}
+
diff --git a/src/main/java/com/pt5/pthouduan/config/TorrentInfoHashPrinter.java b/src/main/java/com/pt5/pthouduan/config/TorrentInfoHashPrinter.java
new file mode 100644
index 0000000..73d0d23
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/config/TorrentInfoHashPrinter.java
@@ -0,0 +1,24 @@
+package com.pt5.pthouduan.config;
+
+import com.turn.ttorrent.tracker.TrackedTorrent;
+import java.io.File;
+
+public class TorrentInfoHashPrinter {
+ public static void main(String[] args) throws Exception {
+ File torrentFile = new File("D:\\torrenttest\\111111\\output.torrent");
+ TrackedTorrent torrent = TrackedTorrent.load(torrentFile);
+ byte[] infoHash = torrent.getInfoHash();
+
+ // 打印十六进制形式的 info_hash
+ System.out.println("info_hash (hex): " + bytesToHex(infoHash));
+ }
+
+ private static String bytesToHex(byte[] bytes) {
+ StringBuilder sb = new StringBuilder();
+ for (byte b : bytes) {
+ sb.append(String.format("%02X", b));
+ }
+ return sb.toString();
+ }
+}
+
diff --git a/src/main/java/com/pt5/pthouduan/config/TrackerConfig.java b/src/main/java/com/pt5/pthouduan/config/TrackerConfig.java
new file mode 100644
index 0000000..93784b5
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/config/TrackerConfig.java
@@ -0,0 +1,93 @@
+package com.pt5.pthouduan.config;
+
+import com.pt5.pthouduan.entity.StatsRecorder;
+import com.pt5.pthouduan.entity.TrackerInitializer;
+import com.pt5.pthouduan.entity.TrackeredTorrentWithStats;
+import com.pt5.pthouduan.service.impl.updatePeerStatsService;
+import com.turn.ttorrent.tracker.TorrentsRepository;
+import com.turn.ttorrent.tracker.TrackedTorrent;
+import com.turn.ttorrent.tracker.Tracker;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+
+import org.springframework.beans.factory.annotation.Value;
+
+import javax.sql.DataSource;
+
+@Configuration
+public class TrackerConfig {
+
+ @Value("${tracker.port:6969}")
+ private int trackerPort;
+
+ @Value("${tracker.accept.foreign:false}")
+ private boolean acceptForeignTorrents;
+
+ @Autowired
+ private DataSource dataSource;
+ @Autowired
+ private TrackerInitializer trackerInitializer;
+ @Autowired
+ private updatePeerStatsService statsService;
+
+ @Bean
+ public Tracker tracker() throws IOException {
+ // 1. 创建 Tracker 实例
+ //Tracker tracker = new Tracker(6969);
+ //Tracker tracker = new Tracker(6969,"127.0.0.1");
+ Tracker tracker = trackerInitializer.createTracker();
+ // 4. 启动 Tracker(true 表示作为守护线程运行)
+ tracker.start(true);
+
+ // 2. 自动加载目录下的所有种子文件
+ File torrentsDirectory = new File("D:\\torrenttest\\111111");
+ if (torrentsDirectory.exists() && torrentsDirectory.isDirectory()) {
+ FilenameFilter filter = (dir, name) -> name.endsWith(".torrent");
+
+ for (File torrentFile : torrentsDirectory.listFiles(filter)) {
+ try {
+ TrackedTorrent baseTorrent = TrackedTorrent.load(torrentFile);
+ //获取HashInfo
+ byte[] infoHash = baseTorrent.getInfoHash();
+ System.out.println("Registered torrent hash: " + infoHash);
+ System.out.println("Registered torrent hash: " + baseTorrent.getHexInfoHash());
+ TrackeredTorrentWithStats trackedTorrentWithStats = new TrackeredTorrentWithStats(infoHash,dataSource,"",statsService);
+ tracker.announce(trackedTorrentWithStats);
+ //tracker.announce(TrackedTorrent.load(torrentFile));
+ System.out.println("Registered torrent: " + torrentFile.getName());
+ } catch (IOException e) {
+ System.err.println("Failed to load torrent: " + torrentFile.getName());
+ e.printStackTrace();
+ }
+ }
+ }
+
+ // 3. 设置是否接受未知种子
+ tracker.setAcceptForeignTorrents(true);
+
+
+
+ System.out.println("Tracker started on port " + trackerPort +
+ ", accepting foreign torrents: " + acceptForeignTorrents);
+
+ // 添加JVM关闭钩子确保Tracker正常停止
+ Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+ tracker.stop();
+ System.out.println("Tracker stopped gracefully");
+ }));
+
+ System.out.println("All registered torrents:");
+ for (TrackedTorrent t : tracker.getTrackedTorrents()) {
+ System.out.println(t.getHexInfoHash());
+ }
+
+
+ return tracker;
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/pt5/pthouduan/config/WebMvcConfig.java b/src/main/java/com/pt5/pthouduan/config/WebMvcConfig.java
new file mode 100644
index 0000000..85145f4
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/config/WebMvcConfig.java
@@ -0,0 +1,23 @@
+package com.pt5.pthouduan.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class WebMvcConfig implements WebMvcConfigurer {
+
+ @Override
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ // 帖子图片:D:/postuploads/ → /images/**
+ registry.addResourceHandler("/images/**")
+ .addResourceLocations("file:D:/postuploads/");//路径改一下
+
+ // ✅ 求助帖图片:D:/uploads/ → /uploads/**
+ registry.addResourceHandler("/uploads/**")
+ .addResourceLocations("file:D:/uploads/");
+ }
+
+
+}
+