07-30 17:10:28.849: W/System.err(14900): java.io.FileNotFoundException: /testnaveen: open failed: EROFS (Read-only file system)
07-30 17:10:28.849: W/System.err(14900): at libcore.io.IoBridge.open(IoBridge.java:409)
07-30 17:10:28.849: W/System.err(14900): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-30 17:10:28.849: W/System.err(14900): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
07-30 17:10:28.849: W/System.err(14900): at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
07-30 17:10:28.849: W/System.err(14900): at com.mobilecem.atms.GlobalFunction.downloadFileFromServer(GlobalFunction.java:147)
我这样做
public static void downloadFileFromServer(String filename, String urlString) throws MalformedURLException, IOException
{
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
URL url = new URL(urlString);
in = new BufferedInputStream(url.openStream());
fout = new FileOutputStream(filename);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1)
{
fout.write(data, 0, count);
System.out.println(count);
}
}
finally
{
if (in != null)
in.close();
if (fout != null)
fout.close();
}
System.out.println("Done");
}
我称之为双向,但同样的错误原因是什么?
GlobalFunction.downloadFileFromServer("test", "http://www.example.com/inputParameters.js");
GlobalFunction.downloadFileFromServer( new File(Activity.this.getFilesDir(),"www").getAbsolutePath(), "url");
答案 0 :(得分:0)
如果您使用的是HONEYCOMB或更高版本,则需要使用AsyncTask来调用与网络相关的操作。
答案 1 :(得分:0)
使用下载管理器。 下载管理器的优势:
它会在导航栏中显示已下载状态及进度状态
package com.example.testing;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class FileDownloading extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startDownload("http://www.example.com/inputParameters.js","inputParameters.js");
}
public void startDownload(String url ,String fileName)
{
Log.d("Download Url", url);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle("Your File Name");
request.setDescription(fileName+" is downloading..");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long downloadID = downloadManager.enqueue(request);
}
}
请勿忘记在manifest.xml