0

I'm trying to download a file from an URL, but when my file is saved the name is different than it should be when it contains special characters. Here is the code I'm using

URL url = new URL(fileURL);

    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

    int responseCode = httpConn.getResponseCode();

    // always check HTTP response code first
    if (responseCode == HttpURLConnection.HTTP_OK) {
        String fileName = "";
        String disposition = httpConn.getHeaderField("Content-Disposition");
        String contentType = httpConn.getContentType();
        int contentLength = httpConn.getContentLength();

        if (disposition != null) {
            // extracts file name from header field
            int index = disposition.indexOf("filename=");
            if (index > 0) {
                fileName =  disposition.substring(index + 10,
                        disposition.length() - 1);
            }
        } else {
            // extracts file name from URL
            fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                    fileURL.length());
        }

        System.out.println("Content-Type = " + contentType);
        System.out.println("Content-Disposition = " + disposition);
        System.out.println("Content-Length = " + contentLength);



        // opens input stream from the HTTP connection
        InputStream inputStream = httpConn.getInputStream();
        String saveFilePath = saveDir + File.separator + fileName;

        // opens an output stream to save into file
        FileOutputStream outputStream = new FileOutputStream(saveFilePath);

        int bytesRead = -1;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        outputStream.close();
        inputStream.close();

        System.out.println("Fichier téléchargé");
    } else {
        System.out.println("Pas de fichier trouvé à l'URL: " + fileURL + ". 
 HTTP code retourné par le serveur: " + responseCode);
    }
    httpConn.disconnect();
}

This is the name in the server "&éù%$£µ#çàè.png" and this is the name I get after download "&éù%$£µ#çàè.png"

What I'am getting wrong? PS: here is an example of the output of my code

Répertoire de sauvegarde: C:\Data\downloads
Content-Type = image/png
Content-Disposition = attachment; filename="&éù%$£µ#çàè.png"
Content-Length = 2208482

Thanks in advance!

  • What server are you using? You were using ISO-8859-1 ([checked here](http://www.webatic.com/run/chars/charset_decoder.php)) [Can you switch to UTF-8?](https://stackoverflow.com/a/7048774/4906586) – Al-un Sep 22 '17 at 19:18
  • Thanks for your reply, It is resolved by always ensuring that the saved fileName is UTF-8 encoding: `fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");` – user8656379 Sep 23 '17 at 10:34
  • Glad to see this. I was asking about the server because I'm working under GlassFish and you can configure the default encoding in glassfish-web.xml. This is one solution, you have other solutions such as Filter. – Al-un Sep 25 '17 at 08:08

0 Answers0