Mr Jedi
Mr Jedi

Reputation: 34705

Wicket WebResponse Attachment Header with UTF-8 charackers?

In wicket project I use request handler for file downloading. Everything is works good until I have file with non standard encoding like utf-8.

I use this code for generate response

@Override
   public void respond(IRequestCycle requestCycle)
   {
      WebResponse response = (WebResponse) requestCycle.getResponse();
      response.setAttachmentHeader(briefcaseDocument.getName());
      response.setContentType(briefcaseDocument.getMimeType());
      response.setContentLength(briefcaseDocument.getSize());
      InputStream inputStream = null;
      OutputStream outputStream = null;
      try
      {
         inputStream = briefcaseDocument.getInputStream();
         outputStream = response.getOutputStream();
         IOUtils.copyLarge(inputStream, outputStream);
      }
      catch (Exception e)
      {
         throw new RuntimeException("unable to push file content", e);
      }
      finally
      {
         IOUtils.closeQuietly(inputStream);
         IOUtils.closeQuietly(outputStream);
      }
   }

but if document has special characketrs I get empty file name. For example:

ćććć is:

Content-Disposition attachment; filename="   ";

ćwiek is:

Content-Disposition attachment; filename=" wiek";

I read this stack topic and change code like this:

 String fileName = briefcaseDocument.getName();
 String encoded = URLEncoder
         .encode(briefcaseDocument.getName(), "UTF-8");

 response.setHeader("Content-Disposition","attachment" +
 ((!Strings.isEmpty(fileName)) ? ("; filename=\"" + fileName + "\"; filename*=UTF-8''"+encoded) : "" ));

and then I get as result:

Content-Disposition attachment; filename="  "; filename*=UTF-8''%C4%87%C5%BC

and file name present good, but I don't feel good with this solution ;) How to make wicket set file name properly?

Upvotes: 0

Views: 479

Answers (1)

martin-g
martin-g

Reputation: 17533

According to http://greenbytes.de/tech/tc2231/ this is the proper way.

Since 7.0.0-M1 (https://issues.apache.org/jira/browse/WICKET-4934) Wicket does this automatically for you.

Upvotes: 3

Related Questions