程序冻结,而下载文件在安卓



我在下面的代码中得到malformmedurlexception,我不知道,是什么导致的。

public void down(String url)
    {     try {
        URL url1 = new URL(url);

        HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();   
        File SDCardRoot = Environment.getExternalStorageDirectory();
        //create a new file, specifying the path, and the filename
        //which we want to save the file as.
        File file = new File(SDCardRoot,"somefile.ext");

        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        int totalSize = urlConnection.getContentLength();
        int downloadedSize = 0;
        byte[] buffer = new byte[1024];
        int bufferLength = 0; //used to store a temporary size of the buffer
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //report progress

        }
        fileOutput.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

表示未知协议。在阅读部分出现之前,连接是完全正常的,在此之前,代码甚至可以打印正确大小的文件。此外,我试图下载的文件有一个urlhttp://download12.aomethin.com/blaa-blaa如果我尝试添加www,请求开始重定向。虽然我认为这可能是一个noobish,但我也想知道如何获得这个文件的名称和保存文件的名称,而不是我选择的一个。

编辑:程序正在工作,现在我只需要知道我如何得到文件的正确名称。设置为后台进程

在主线程上下载不是一个好的做法;你应该在一个新线程中实现它;因为在下载时,主线程忙于下载文件。如果等待时间超过预期,系统将产生"Not responding"错误。

为了做到这一点,你可以使用"TaskAsync"或"IntentService",甚至创建一个新线程。不过,我建议[TaskAsync],因为它很容易实现。

自己设置文件名是一个很好的做法:

public void downloadClicked() {
    InputStream in = null;
    FileOutputStream f = null;      
    try {
        String path ="ftp://administrator:password@131.164.140.118:21/MyShedule.zip";
        String targetFileName = "MyShedule.zip";
        URL u = new URL(path);
        URLConnection c =  u.openConnection();         
        c.setDoOutput(true);
        c.connect();
        String string = Environment.getExternalStorageDirectory().getPath().toString();
        in = c.getInputStream();
        f = new FileOutputStream(new File(string +"/kidsLibrary/"+ targetFileName));
        IOUtils.copy(in, f);
        } catch (MalformedURLException e) {
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();            
        } catch (ProtocolException e) {         
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();
        } catch (FileNotFoundException e) {        
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();
        } catch (IOException e) {           
            Toast.makeText(KidsLibrary.this, e.toString(), Toast.LENGTH_LONG)
            .show();
    } finally {
          IOUtils.closeQuietly(in);
          IOUtils.closeQuietly(f);
        }   
}

相关内容

  • 没有找到相关文章

最新更新