get the name of a pdf file that is on the server

0

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();
    }
    
asked by mjuan 30.08.2017 в 17:34
source

1 answer

0

You are actually getting the name of the original file from the url by:

 String fileName = Uri.parse(url).getLastPathSegment();

for example if your url is:

String url = "http://www.example.com/some/path/to/a/file.pdf"

By Uri.parse(url).getLastPathSegment() you get

file.pdf

But the method to save used to save the file is createTempFile ()

file = File.createTempFile(fileName,null,getApplicationContext().getFilesDir());

which requires a prefix, a suffix and the destination path.

  

createTempFile (String prefix, String Suffix, File Directory)

     
  • Prefix - The prefix string that will be used to generate the file name; Must have at least three characters.

  •   
  • Suffix - The suffix string that will be used to generate the file name; It can be null, in which case the suffix will be used   ".tmp"

  •   
  • Directory - The directory where the file will be created, or null if the temporary file directory is to be used by default

  •   

In this case it is not possible to change the name of the "temporary file" to be created , only add a prefix and a suffix:

As an example, I have an application that uses this method and you can notice I define a prefix ("android_tutorial") and a suffix (".pdf"):

 file = File.createTempFile("android_tutorial", ".pdf", getApplicationContext().getFilesDir());

therefore the prefix and suffix are added but there is no way to change the name of the temporary file that the operating system creates:

    
answered by 30.08.2017 в 18:10