gpt4 book ai didi

java - 下载和合并期间文件损坏

转载 作者:行者123 更新时间:2023-11-30 07:55:50 25 4
gpt4 key购买 nike

我有java代码,其目的是使用http请求下载一小部分视频文件(例如两部分大小相等),稍后需要在相同条件下合并文件,以便我取回原始文件。但我在合并时收到损坏的文件。除此之外,合并文件的大小也比原来增加了。

文件读取:

    public static int ReadFiles(String address,int StartPos,
int LastPos,String filename) throws IOException
{
// Create the byte array to hold the data
byte[] bytes = new byte[(int)LastPos];

try {
url = new URL(address);

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Opening connection to " + address + "...");
System.out.println("Start Address " + StartPos + " End Postion "+LastPos);
URLConnection urlC = null;
try {
urlC = url.openConnection();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
urlC.setRequestProperty("User-Agent","");
urlC.connect();

InputStream is = urlC.getInputStream();
FileOutputStream fos = null;

fos = new FileOutputStream(filename);
int oneChar,CharRead;

System.out.println("Downloading bytes: ");

is.skip(StartPos);

while (((oneChar = is.read()) != -1) ) {
// System.out.print((char)oneChar);
fos.write(oneChar);
StartPos++;
System.out.print("\r"+StartPos);
if((StartPos>=LastPos))
{
System.out.println("Downloading break at : "+StartPos+"="+LastPos);
break;

}
// if(count==25260)break;
}


is.close();
fos.close();
//System.out.println(StartPos+ " byte(s) copied");
System.out.println("File Downloader Completed Work!");

return 0;
}

合并:

public static void mergeFiles(File[] files, File mergedFile) {

FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(mergedFile, true);
out = new BufferedWriter(fstream);
} catch (IOException e1) {
e1.printStackTrace();
}

for (File f : files) {
System.out.println("merging: " + f.getName());
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));

String aLine;
while ((aLine = in.readLine()) != null) {
out.write(aLine);
out.newLine();
}

in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}

最佳答案

对于部分下载,您可以使用 Range 请求属性(如果服务器支持 HTTP 1.1):Java: resume Download in URLConnection

合并期间请不要使用 Reader,因为它适用于字符而不是字节。您可以使用这样的函数来代替:

public static void copyStream(InputStream is, OutputStream os) throws IOException {
byte[] buffer = new byte[10000];
while (true) {
int len = is.read(buffer);
if (len < 0)
break;
if (len > 0)
os.write(buffer, 0, len);
}
}

所以 for 循环将是这样的:

for (File f : files) {
System.out.println("merging: " + f.getName());
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
copyStream(fis, out);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null)
fis.close();
}
}

PS:流复制操作也有一些标准实现。例如:http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html#copy(java.io.InputStream,%20java.io.OutputStream)

关于java - 下载和合并期间文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com