2

I´m trying to serve binary response in my java servlet. First, I get the requested data from database and than try to set it as a response like this:

    ResultSet rset = (ResultSet) stmt.executeQuery();

    if (rset.next())
    {
      byte[] bData = rset.getBytes("Soubor");
      String sJmenoSouboru = rset.getString("Jmeno_souboru");

      response.setHeader("Content-Disposition","attachment;filename=" + sJmenoSouboru);
      //response.setHeader("Content-Description", sJmenoSouboru);
      response.setHeader("Content-Transfer-Encoding", "binary");
      //response.setContentType("application/pdf");
      response.setContentType("application/octet-stream");

      ServletOutputStream hOutStream = response.getOutputStream();
      hOutStream.write(bData);
      hOutStream.flush();
      hOutStream.close();
    }

This works fine, until there is a '§' character in file name. Than i get ERR_SPDY_PROTOCOL_ERROR. Mentioned character should be usable in file names as far as I know. Does anybody know, where could be the problem?

Mono
  • 206
  • 1
  • 21
  • Have you tried it in a different browser? Isn't Avast running on your computer? See https://support.google.com/chrome/forum/AAAAP1KN0B0sfAqeo4hDy8/?hl=en&gpf=%23!topic%2Fchrome%2FsfAqeo4hDy8 – Jozef Chocholacek Jul 08 '19 at 12:17
  • Method getNString is not supported on postgreSQL database, I tried that and I was getting MethodNotSupported exception. – Mono Jul 08 '19 at 12:22
  • I tried that with firefox and chrome and the result is the same. Good point with Avast, I'm using it, but I tried to disable main shields with no changes in browser behavior in this case. – Mono Jul 08 '19 at 12:25
  • 1
    Take a look at [this post](https://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http). In short, you're using a filename with a non-ASCII character. – stdunbar Jul 08 '19 at 16:03

1 Answers1

0

As stdunbar mentioned in comments, the problem is that '§' is a non-ASCII character, that needs to be escaped in file name. I solved it by changing this line of code:

String sJmenoSouboru = URLEncoder.encode(rset.getString("Jmeno_souboru"), StandardCharsets.UTF_8.toString());
Mono
  • 206
  • 1
  • 21