I need to download a pdf from my android app but without stepping on the name, that is, when I enter "url /.../.../valortiempo"
to generate the request to the server, it will reply with a pdf with its name, which refers to that time I send you.
The question is that in my code it asks me to enter a name to rename my file, and what I need is the original name of the pdf:
String url = "";
try {
byte[] todo = null;
byte[] parte = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
URL newurl = new URL(url);
HttpURLConnection con =
(HttpURLConnection)newurl.openConnection();
con.connect();
int cont =0;
while ((cont = con.getInputStream().read(parte)) != -1) {
bos.write(parte, 0, cont);
bos.flush();
publishProgress(cont);
}
todo = bos.toByteArray();
File file;
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName,null,getApplicationContext().getFilesDir());
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bost = new BufferedOutputStream(fos);
bost.write(todo);
listaDeArchivos.add(file);
bost.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}