文件在下载和合并期间损坏

时间:2015-09-21 02:37:26

标签: java

我有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();
    }

1 个答案:

答案 0 :(得分:1)

对于部分下载,您可以使用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 (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)