Dear,
I think there is a concept error. The servers deliver the content from a socket as a stream of bytes, and it is the client application that decides what to do with it or what name to put in the file where it is stored.
TAKING THE NAME FROM THE INDICATED URL:
If the name you want to put depends on the suffix of the call to the URL that you indicate, I suggest you use something like that
try {
String urlDescarga = urlFragment[0];
byte[] todo = null;
byte[] parte = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
URL newurl = new URL(urlDescarga);
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();
// Determinamos el sufijo. Suponiendo que el url es:
// http://www.example.com/some/path/to/a/14041111001
// El sufijo será:
// 14041111001
int p=urlDescarga.lastIndexOf("/");
String sufix=urlDescarga.substring(p+1);
// Generamos el nombre del archivo usando el sufijo.
// Si el sufijo es 14041111001 el nombre del archivo será
// cuenta-num14041111001.pdf
String name = "cuenta-num" + sufix + ".pdf";
File file = new File(context.getFilesDir(), name);
if (!file.exists()) {
file.createNewFile();
}
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();
}
TAKING THE NAME GENERATED DYNAMICALLY FROM THE SERVER:
If you want the server to be the one that generates the name and do not want to use an exclusive service for that (for example, one that returns the file name that corresponds to that code), a very simple way to do it is to send it from the server within the first, say, 40 bytes of the stream completed with spaces, and that is the first thing you read of it in Android to then send the rest of the content. That way you can take it as follows:
try {
String urlDescarga = urlFragment[0];
byte[] todo = null;
byte[] parte = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
URL newurl = new URL(urlDescarga);
HttpURLConnection con =
(HttpURLConnection) newurl.openConnection();
con.connect();
int cont =0;
// Primero leemos el nombre del archivo dentro de los primeros 40 bytes:
// Digamos que se envia (sin comillas) 'cuenta-num14041111001.pdf '
String name = "";
int nameSize = 40;
if((cont = con.getInputStream().read(parte,0,nameSize)) != -1)
name = new String(parte).trim();
// Luego continuas leyendo el contenido del archivo:
while ((cont = con.getInputStream().read(parte)) != -1) {
bos.write(parte, 0, cont);
bos.flush();
publishProgress(cont);
}
todo = bos.toByteArray();
File file = new File(context.getFilesDir(), name);
if (!file.exists()) {
file.createNewFile();
}
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();
}
Error handling can be improved, but technically it can give you a good idea of how to solve it.
I hope it's useful for you. Greetings!