我有一个应用程序,我想从我的私人服务器更新。下面的代码应该从服务器下载新的apk文件并将其写入sdcard,应用程序可以在其中更新自己。
我收到了FileNotFoundException。该文件确实存在于服务器上。
为什么会发生这种情况的任何想法?
由于
public void updateApp(String version){
try {
version = version.trim();
String urlWithVersion = "http://cfweb.yourofficeanywhere.co.uk/entryactivityv" + version + ".apk";
URL url = new URL(urlWithVersion);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
Log.e(TAG, " HttpURLConnection = connected");
String PATH = Environment.getExternalStorageDirectory() +"/";
File file = new File(PATH);
file.mkdirs();
Log.e(TAG, "mkdirs()");
File outputFile = new File(file, "app.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
Log.e(TAG, "fos");
InputStream is = c.getInputStream();
Log.e(TAG, "c.getInputStream()");
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
Log.e(TAG, "update file size = " + len1);
fos.write(buffer, 0, len1);
}
Log.e(TAG, "update file size = " + len1);
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
NfcscannerActivity.this.startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "update error = " + Log.getStackTraceString(e));
}
} //end of updateApp
09-10 16:00:59.504: E/NfcscannerActivity(18814): HttpURLConnection = connected
09-10 16:00:59.504: E/NfcscannerActivity(18814): mkdirs()
09-10 16:00:59.504: E/NfcscannerActivity(18814): fos
09-10 16:00:59.644: E/MP-Decision(2194): Update arg 1
09-10 16:01:00.674: E/NfcscannerActivity(18814): update error = java.io.FileNotFoundException: http://cfweb.yourofficeanywhere.co.uk/entryactivityv33.apk
09-10 16:01:00.674: E/NfcscannerActivity(18814): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at com.carefreegroup.rr3.NfcscannerActivity.updateApp(NfcscannerActivity.java:6353)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at com.carefreegroup.rr3.NfcscannerActivity$AsyncUpdateApp.doInBackground(NfcscannerActivity.java:6317)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at com.carefreegroup.rr3.NfcscannerActivity$AsyncUpdateApp.doInBackground(NfcscannerActivity.java:1)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-10 16:01:00.674: E/NfcscannerActivity(18814): at java.lang.Thread.run(Thread.java:811)