下载XML但没有这样的文件或目录

时间:2017-05-18 11:04:49

标签: android file download

我对这一切都很陌生,所以请原谅我。我试图将XML文件下载到设备,然后可以对其进行解析。我目前的代码是 -

public class DownloadFromURL implements Runnable {
    private static final int BUFFER_SIZE = 4096;

    @Override
    public void run() {
        Log.i("Download From URL", "Run");

        String fileURL = "http://m.highways.gov.uk/feeds/rss/AllEvents.xml";
        String saveDir = "/data/data/com.androidandyuk/files";
        URL url;
        try {
            url = new URL(fileURL);

            HttpURLConnection httpConn = (HttpURLConnection) 
            url.openConnection();

            int responseCode = httpConn.getResponseCode();
            Log.i("Download from URL", "Resp Code :" + responseCode);
            // always check HTTP response code first
            if (responseCode == HttpURLConnection.HTTP_OK) {
                String fileName = "";
                String disposition = httpConn.getHeaderField("Content-Disposition");
                String contentType = httpConn.getContentType();
                int contentLength = httpConn.getContentLength();

                if (disposition != null) {
                    // extracts file name from header field
                    int index = disposition.indexOf("filename=");
                    if (index > 0) {
                        fileName = disposition.substring(index + 10,
                                disposition.length() - 1);
                    }
                } else {
                    // extracts file name from URL
                    fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                            fileURL.length());
                }

                // opens input stream from the HTTP connection
                InputStream inputStream = httpConn.getInputStream();
                String saveFilePath = saveDir + File.separator + fileName;
                Log.i("Download from URL", "Opening a Stream");
                // opens an output stream to save into file
                FileOutputStream outputStream = new 
                FileOutputStream(saveFilePath);

                int bytesRead = -1;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                outputStream.close();
                inputStream.close();

                Log.i("Download", "File downloaded");
            } else {
                Log.i("Download", "No file to download. Server replied HTTP code: " + responseCode);
            }

            httpConn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

然后由 -

运行
DownloadFromURL getTraffic = new DownloadFromURL();
            Thread t = new Thread(getTraffic);
            t.start();

但是当它运行时,我收到一个错误,上面写着' java.io.FileNotFoundException:/ data / data / com.androidandyuk / files / AllEvents.xml(没有这样的文件或目录)'

有人可以帮忙吗?

(如果你想对我进行投票,请至少评论为什么,因为即使我觉得我已经很好地格式化了我的问题,我也会投票...并且已经做了很多寻找答案!谢谢)

0 个答案:

没有答案