Search code examples
javaandroidfiledownloadepub

Text file is getting downloaded from web but issue with ePub/Pdf


I'm referring following code to download a file from particular URL.

public class FileFromServerExample extends Activity {

    static String PACKAGE_NAME;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        PACKAGE_NAME = getApplicationContext().getPackageName();        

        File folder = new File("/data/data/"
                + FileFromServerExample.PACKAGE_NAME + "/ePub/");
        boolean created = folder.isDirectory();
        if (!created) {
            folder.mkdir();
        }

        File file = new File(folder, "Sample.epub");
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Downloader
                .DownloadFile(
                        "http://www.google.co.in/url?sa=t&rct=j&q=sample%20epub%20filetype%3Aepub&source=web&cd=2&ved=0CFMQFjAB&url=http%3A%2F%2Fdl.dropbox.com%2Fu%2F1177388%2Fflagship_july_4_2010_flying_island_press.epub&ei=i5gHUIOWJI3RrQeGro3YAg&usg=AFQjCNFPKsV-tieF4vKv7BXYmS-QEvd7Uw",
                        file);
}
}

Downloader.java

public class Downloader {

    public static void DownloadFile(String fileURL, File directory) {
        try {

            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I'm getting Sample.epub at desired location.
But when I try to open that file, I got an error message stating Not a valid zip file.

I tried with various URL's, with Pdf instead of ePub (it gives file corrupted error), but the same problem persists.

However, when I tried to download a simple text file from this sample link , it works properly.

So by common sense, it appears that this URL to download ePub might be broken, but that's not the case as I successfully downloaded the ePub if I visit the link from my PC's browser.
However when I try to download the ePub from this link in android application, it doesn't work.

Any idea, where I'm going wrong? Any help appreciated.


Solution

  • this is because you are not pointing to a direct url. instead in browsers, google redirects to the original url. in your case the original link is:

    http://dl.dropbox.com/u/1177388/flagship_july_4_2010_flying_island_press.epub

    if you try to download this direct url, you will succeed.