我正在尝试以编程方式从已提供网址的api下载文件,但下载的文件总是不完整,其大小小于我手动下载的文件的网址...
这是我的代码:
try{
URL url = new URL(filesToBeDwlUrl);
HttpURLConnection c = (HttpURLConnection)url.openConnection();
c.setRequestMethod("GET");
c.connect();
//if connection response is not OK then log
if(c.getResponseCode() != HttpURLConnection.HTTP_OK){
Log.e("Response", "Server returned HTTP "+ c.getResponseCode());
}
storagePath = new File(mContext.getFilesDir().getAbsoluteFile() + "/"+ "AudiosArchiveFolder");
if(!storagePath.isDirectory())
storagePath.mkdir();
Log.v("Download-info ", "Directory created in internal storage : "+ storagePath);
outputZipFile = new File( storagePath, fileName);
FileOutputStream fos = new FileOutputStream(outputZipFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1;
while((len1 = is.read(buffer)) != -1){
fos.write(buffer, 0, len1);
}
fos.flush();
fos.close();
is.close();
}catch (Exception e){
mProgressDialog.dismiss();
outputZipFile = null;
Toast.makeText(mContext, "An error occurred. Multimedia content could not be saved on your device. Please contact customer support", Toast.LENGTH_SHORT).show();
Log.e("Download-error", "Error found: "+e.getMessage());
}
url filesToBeDwlUrl值为:"http://play.digtransformations.com/api.custom.action.execute/YUBhLmFh/YWE=/81GQ/zbfd874/O8npdvQbJmexu8Hq/j8Hs";
我还不知道问题是否与我的代码有关,或者是否与priovided网址有关,因为我可以在手动下载文件时获取正确的压缩文件。
感谢您的帮助!
答案 0 :(得分:0)
问题在于从网站获取zip文件。在android中编写部分是好的。当我尝试从网址获取内容类型时,我会收到&text; html'的内容类型。当我在记事本中打开下载的文件(.zip文件)时,我得到了一个包含所有HTML代码的html页面。问题来自写入文件的网站。在网站中,应妥善维护内容类型。