Jump to content

[Resolvido] obter extensão do ficheiro antes de fazer o download


Recommended Posts

Posted

boas pessoal eu tenho aqui um problema e não sei como resolver.

eu uso uma classe para fazer download de ficheiros

mas o url não é do tipo

http://site.php?ficheiro.rar

o url é http://site.php?ficheiro=download

onde é precisso usar o metodo Post para fazer o download do ficheiro.

mas o problema é que por vezes o ficheiro é rar e outras vezes é .zip

protected String doInBackground(String... aurl) {

try {
	HttpClient httpClient = new DefaultHttpClient();
	  HttpPost httpPost = new HttpPost("http://www.legendas-zone.org/downloadsub.php");
	  httpPost.addHeader("Cookie", login.cooklogin);
	  BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("sid", aurl[0]);
	BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("action", "download");
	List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
	nameValuePairList.add(usernameBasicNameValuePair);
	nameValuePairList.add(passwordBasicNameValuePAir);
	UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
	httpPost.setEntity(urlEncodedFormEntity);
	HttpResponse httpResponse = httpClient.execute(httpPost);
	long lenghtOfFile = httpResponse.getEntity().getContentLength();
	FileOutputStream f = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/download/", aurl[1]));
	InputStream in = httpResponse.getEntity().getContent();
	byte[] buffer = new byte[1024];
	int len1 = 0;
	long total = 0;
	while ((len1 = in.read(buffer)) > 0) {
		total += len1;
		publishProgress("" + (int)((total*100)/lenghtOfFile));
		f.write(buffer, 0, len1);
	}
	f.close();
} catch (Exception e) {
	Log.d("Downloader", e.getMessage());	  
}

return null;

}

com calma e sem stresses

Posted

eis a solução para quem estiver com o mesmo problema


Header headers[] = httpResponse.getAllHeaders();
       for(Header h:headers){
           System.out.println("teste" + h.getName() + ": " + h.getValue());
       }

o nome do ficheiro com a extensão tal e qual como fosse ser trasferido do navegador está

a frente da palavra content-disposition

com calma e sem stresses

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.