如何从android eclipse中的本地主机系统下载文件?

时间:2015-07-28 05:54:52

标签: java android eclipse

我可以从浏览器下载文件,但是我无法下载保存在本地主机系统中的文件,你们可以帮我解决这个问题。 以下是我的代码和错误:

URL url = new URL("http://30.30.30.38:51749/Content/AdminUploads/close.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);

//connect
urlConnection.connect();

//set the path where we want to save the file    
File SDCardRoot = Environment.getExternalStorageDirectory(); 
//create a new file, to save the downloaded file 
File outputFile;
File wallpaperDirectory = new File("/sdcard/Downloads/");
if(!wallpaperDirectory.exists()){
    wallpaperDirectory.mkdirs();
    outputFile = new File(wallpaperDirectory, fileName);
}else{
    outputFile = new File(wallpaperDirectory, fileName);
}
if(outputFile.exists()){
    outputFile.delete();
}
FileOutputStream fileOutput = new FileOutputStream(outputFile);

//Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();

//this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();

Thread timer = new Thread() {
            @Override
            public void run() {
getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    pb.setMax(totalSize);
                    }
                });
  }
        };
        timer.start();

//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;

while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Thread times = new Thread() {
                @Override
                public void run() {
//do something
    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
            pb.setProgress(downloadedSize);
            float per = ((float)downloadedSize/totalSize) * 100;
            //cur_val.setText("Downloaded " + downloadedSize + "KB / " + totalSize + "KB (" + (int)per + "%)" );
                        }
                    });
      }
            };
            times.start();
}
//close the output stream when complete //
fileOutput.close();;  
Thread time = new Thread() {
            @Override
            public void run() {
//do something
getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    dialog.dismiss();// if you want close it..
            Toast.makeText(getActivity(), "Downloaded in Required path", Toast.LENGTH_LONG).show();
                    }
                });
  }
        };
        time.start();

} catch (final MalformedURLException e) {
showError("Error : MalformedURLException " + e); 
dialog.dismiss();
e.printStackTrace();
} catch (final IOException e) {
showError("Error : IOException " + e);  
dialog.dismiss();
e.printStackTrace();
}
catch (final Exception e) {
showError("Error : Please check your internet connection " + e);
dialog.dismiss();
} 

错误

java.io.FileNotFoundException:http://30.30.30.38:51749/Content/AdminUploads/close.png

0 个答案:

没有答案