0

I can download data via HttpUrlConnection and InputStream but I need to download raw-data. So, i want to create a DownloadManager via raw-data, then using raw-data I convert this data to binary or image format. According to my research, I see "download file from url" but I can't download file in mac? Always, I get FileNotFoundException. Please help me. How I can download data from url?

public class DownloadData extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {

    try {
        downloadData("https://blablalabla/get");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public void downloadData(String myurl) throws IOException {
    URL u = new URL(myurl);
    InputStream is = u.openStream();

    DataInputStream dis = new DataInputStream(is);

    byte[] buffer = new byte[1024];
    int length;

    OutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/Users/ilknurpc/Desktop/text.docx"));
    while ((length = dis.read(buffer))>0) {
        fos.write(buffer, 0, length);
    }


}
}
nocmmnt
  • 3
  • 6

1 Answers1

0

If you want to construct a workable download manager, I would suggest that you take a look at the Tomcat Default Servlet Implementation .

There a few number of HTTP headers that you need to understand such as E-Tags and Http Range Headers for a proper implementation.

Thankfully the Tomcat Default Servlet handles the prerequisites for you.

You can adapt this servlet in your code with minor changes (package declaration etc).

Saikat
  • 548
  • 1
  • 4
  • 12