我想从 URL 下载图像文件。我的代码适用于互联网上图像的任何 URL,但我找不到使用其 URL(通过 WiFi 热点连接到我的 Android 手机)从我自己的 PC 下载文件的方法。这可能吗?如果是的话,请告诉我怎么做。
URL url = new URL("file://192.168.43.198/f:/ur/demo.jpg");
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
}
您的计算机不会对“文件:”协议做出反应。在互联网上您将使用“http:”,因此这里也使用它。在您的电脑上安装网络服务器以使其正常运行。