2

我正在编写一个应用程序来下载种子文件并修改它的跟踪链接(只需替换密码)。但可能我对编码有很大的问题,因为当我保存修改后的文件时,它无法在我的 torrent 客户端中打开。

我写了一个代码来下载和修改文件:

@Override
public void exeucte(String link) throws IOException {
    FileOutputStream fos = null;
    try {
        URL website = new URL(link);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        String fileName = getFileName(link);
        fos = new FileOutputStream(fileName);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        replacePassKey(fileName);
    } finally {
        if (fos != null)
            fos.close();
    }

}

private void replacePassKey(String fileName) throws IOException {
    File originalFile = new File(fileName);
    String lines = readLines(originalFile);
    String replacedLines = lines.replaceAll("(.*passkey=)(.*)(:comment27.*)", "$1" + PASS_KEY + "$3");
    originalFile.delete();
    writeReplacedLines(replacedLines, originalFile);
}

private void writeReplacedLines(String replacedLines, File file) throws IOException {
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(file));
        bw.write(replacedLines);
    } finally {
        if (bw != null)
            bw.close();
    }

}

private String readLines(File originalFile) throws IOException {
    RandomAccessFile raf = null;
    String lines = null;
    try {
        raf = new RandomAccessFile(originalFile, "r");
        byte[] bytes = new byte[(int) raf.length()];
        raf.read(bytes);
        lines = new String(bytes, Charset.forName("UTF8"));
    } finally {
        if (raf != null)
            raf.close();
    }
    return lines;
}

我确信下载是有效的,因为我可以在 torrent 客户端中打开未修改的文件(当我在 KomodoEdit 中修改下载的文件时)。但是当我修改文件并保存替换的字符串时,我的客户端无法打开它并抱怨无效数据。

有人有什么想法吗?也许 UTF8 是错误的,或者我必须更改我的代码的某些部分?

4

1 回答 1

0

尝试使用各种设置打开它并检查wiki 页面

于 2013-12-14T09:44:16.450 回答