问题描述
我想从我的Web服务器上下载 .apk文件
文件,但我越来越
java.io.FileNotFoundException:HTTP: //xxx/mygames/myapks/demo.apk
所有的时间。
不过,该文件是present本网址
下面是我的code,我在做什么。
进口的java.io.File;
进口java.io.FileOutputStream中;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口的java.net.URL;
进口android.content.Context;
进口android.content.Intent;
进口android.net.Uri;
进口android.os.AsyncTask;
进口android.os.Environment;
进口android.util.Log;
公共类InstallAPK扩展的AsyncTask<字符串,太虚,太虚> {
私人上下文的背景下;
公共无效setContext(上下文contextf){
上下文= contextf;
}
@覆盖
保护无效doInBackground(字符串...为arg0){
尝试 {
网址URL =新的URL(将arg0 [0]);
HttpURLConnection的C =(HttpURLConnection类)url.openConnection();
c.setRequestMethod(GET);
c.setDoOutput(真正的);
c.connect();
文件SD卡= Environment.getExternalStorageDirectory();
文件MYDIR =新的文件(SD卡,安卓/数据/ com.sush19.app / TEMP);
myDir.mkdirs();
文件OUTPUTFILE =新的文件(MYDIRtemp.apk);
如果(outputFile.exists()){
outputFile.delete();
}
FileOutputStream中FOS =新的FileOutputStream(OUTPUTFILE);
InputStream的是= c.getInputStream();
byte []的缓冲区=新的字节[1024];
INT LEN1 = 0;
而((LEN1 = is.read(缓冲液))!= - 1){
fos.write(缓冲液,0,LEN1);
}
fos.flush();
fos.close();
is.close();
意向意图=新的意图(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(新文件(SD卡,安卓/数据/ com.sush19.app /温度/ temp.apk)),应用程序/ vnd.android.package存档);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //如果没有这个标志的android返回意图的错误!
context.startActivity(意向);
}赶上(例外五){
Log.e(UpdateAPP+ e.getMessage()Update错误!);
}
返回null;
}
}
在按钮单击我打电话如下
InstallAPK downloadAndInstall =新InstallAPK();
downloadAndInstall.setContext(getApplicationContext());
downloadAndInstall.execute(HTTP://xxx/mygames/myapks/demo.apk);
我试着用文本文件为好,对于我刚上传demo.txt文件的http://xxx/mygames/myapks/demo.txt
我获得同样的异常,但是当我浏览这个demo.txt文件的URL在浏览器中打开。我需要做的,从Web服务器下载的.apk文件。
我还包含所有必需的权限
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=android.permission.READ_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
我收到上述例外的:
的InputStream是= c.getInputStream();
这是不是你的code故障。这是服务器故障。只是尝试从浏览器中点击URL地址。
I want to download .apk
file from my web server, but I'm gettingjava.io.FileNotFoundException: http://xxx/mygames/myapks/demo.apk
all the time.
But the file is present in this URL
Below is my code, What I'm doing.
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
public class InstallAPK extends AsyncTask<String,Void,Void> {
private Context context;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
File sdcard = Environment.getExternalStorageDirectory();
File myDir = new File(sdcard,"Android/data/com.sush19.app/temp");
myDir.mkdirs();
File outputFile = new File(myDir, "temp.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.flush();
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(sdcard,"Android/data/com.sush19.app/temp/temp.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
} catch (Exception e) {
Log.e("UpdateAPP", "Update error! " + e.getMessage());
}
return null;
}
}
On button Click I'm calling as follow
InstallAPK downloadAndInstall = new InstallAPK();
downloadAndInstall.setContext(getApplicationContext());
downloadAndInstall.execute("http://xxx/mygames/myapks/demo.apk");
I tried with text file as well, for that I just uploaded demo.txt file to http://xxx/mygames/myapks/demo.txt
I'm getting same Exception, but when I browse this demo.txt file URL it opens in the Browser. What I need to do to download .apk file from the Web server.
I've also Included all the required permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I'm getting the above Exception at:
InputStream is = c.getInputStream();
It is not your code fault.It is server fault. just try to hit URL address from Browser.
这篇关于无法从Android中远程服务器下载的.apk文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!