gpt4 book ai didi

java - 在android中下载zip文件

转载 作者:行者123 更新时间:2023-12-01 11:34:14 32 4
gpt4 key购买 nike

我有以下代码 fragment ,可将 zip 文件从互联网下载到我的 SD 卡。它会下载原始大小的文件。但我无法提取文件,因为它显示“文件损坏”错误。所有网址都会发生这种情况。

URL url;
URLConnection conn;
int fileSize;
InputStream inStream;
String outFile;
String fileName = "";
OutputStream outStream;
Message msg;
msg = Message.obtain(mhandler, DOWNLOAD_STARTED, 0, 0, downloadUrl);
mhandler.sendMessage(msg);
try {
url = new URL(downloadUrl);
conn = url.openConnection();
if(url.getProtocol().equals("https")){
conn = (HttpsURLConnection) conn;

}
conn.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.addRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
fileSize = conn.getContentLength();
int fileSizeKB = fileSize / 1024;
msg = Message.obtain(mhandler, DOWNLOAD_STARTED, fileSizeKB, 0,
fileName);
mhandler.sendMessage(msg);
inStream = conn.getInputStream();
outFile = Environment.getDataDirectory().getPath()+"/windows/Documents/file.zip";
outStream = new FileOutputStream(outFile);
byte[] data = new byte[1024];
int bytesRead = 0;
while (!isInterrupted()
&& (bytesRead = inStream.read(data)) != -1) {
outStream.write(data, 0, bytesRead);
}
outStream.flush();
outStream.close();
inStream.close();
if (isInterrupted()) {
new File(outFile).delete();
} else {
msg = Message.obtain(mhandler, DOWNLOAD_COMPLETED);
mhandler.sendMessage(msg);
}
} catch (Exception exp) {
msg = Message.obtain(mhandler, DOWNLOAD_FAILED);
mhandler.sendMessage(msg);
}

你能告诉我我做错了什么吗?

最佳答案

尝试使用此代码下载文件:

public class download extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button)findViewById(R.id.startBtn);
startBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startDownload();
}
});
}

private void startDownload() {
String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.zip";
new DownloadFileAsync().execute(url);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}

class DownloadFileAsync extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {
int count;

try {

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.zip");

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}

output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;

}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}

关于java - 在android中下载zip文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163067/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com