How to return downloaded file name in Cyrillic?
I want to return file with Cyrillic name.
Now my code looke like:
@GetMapping("/download/{fileId}")
public void download(@PathVariable Long fileId, HttpServletResponse response) throws IOException {
...
response.setContentType("txt/plain" + "; charset=" + "WINDOWS-1251");
String filename = "русское_слово.txt";
response.addHeader("Content-disposition", "attachment; filename=" + filename);
response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
//...
}
When I access url from browser - browser provides me dialog to save file on disk but it show _ instead of Cyrillic symbols.
Looks like it is response header encoding issue:
{
"access-control-expose-headers": "Content-disposition",
"content-disposition": "attachment; filename=???_??.txt",
"date": "Fri, 28 Dec 2018 15:53:44 GMT",
"transfer-encoding": "chunked",
"content-type": "txt/plain;charset=WINDOWS-1251"
}
I tried following option:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + filename);
and following:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(filename,"UTF-8"));
but it doesn't help
How can I fix this issue?
java spring-mvc encoding download cyrillic
add a comment |
I want to return file with Cyrillic name.
Now my code looke like:
@GetMapping("/download/{fileId}")
public void download(@PathVariable Long fileId, HttpServletResponse response) throws IOException {
...
response.setContentType("txt/plain" + "; charset=" + "WINDOWS-1251");
String filename = "русское_слово.txt";
response.addHeader("Content-disposition", "attachment; filename=" + filename);
response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
//...
}
When I access url from browser - browser provides me dialog to save file on disk but it show _ instead of Cyrillic symbols.
Looks like it is response header encoding issue:
{
"access-control-expose-headers": "Content-disposition",
"content-disposition": "attachment; filename=???_??.txt",
"date": "Fri, 28 Dec 2018 15:53:44 GMT",
"transfer-encoding": "chunked",
"content-type": "txt/plain;charset=WINDOWS-1251"
}
I tried following option:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + filename);
and following:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(filename,"UTF-8"));
but it doesn't help
How can I fix this issue?
java spring-mvc encoding download cyrillic
How did you get the headers the server sent? TheContent-Dispositionheader does not contain the*=UTF-8part
– caco3
Dec 28 '18 at 16:26
add a comment |
I want to return file with Cyrillic name.
Now my code looke like:
@GetMapping("/download/{fileId}")
public void download(@PathVariable Long fileId, HttpServletResponse response) throws IOException {
...
response.setContentType("txt/plain" + "; charset=" + "WINDOWS-1251");
String filename = "русское_слово.txt";
response.addHeader("Content-disposition", "attachment; filename=" + filename);
response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
//...
}
When I access url from browser - browser provides me dialog to save file on disk but it show _ instead of Cyrillic symbols.
Looks like it is response header encoding issue:
{
"access-control-expose-headers": "Content-disposition",
"content-disposition": "attachment; filename=???_??.txt",
"date": "Fri, 28 Dec 2018 15:53:44 GMT",
"transfer-encoding": "chunked",
"content-type": "txt/plain;charset=WINDOWS-1251"
}
I tried following option:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + filename);
and following:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(filename,"UTF-8"));
but it doesn't help
How can I fix this issue?
java spring-mvc encoding download cyrillic
I want to return file with Cyrillic name.
Now my code looke like:
@GetMapping("/download/{fileId}")
public void download(@PathVariable Long fileId, HttpServletResponse response) throws IOException {
...
response.setContentType("txt/plain" + "; charset=" + "WINDOWS-1251");
String filename = "русское_слово.txt";
response.addHeader("Content-disposition", "attachment; filename=" + filename);
response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
//...
}
When I access url from browser - browser provides me dialog to save file on disk but it show _ instead of Cyrillic symbols.
Looks like it is response header encoding issue:
{
"access-control-expose-headers": "Content-disposition",
"content-disposition": "attachment; filename=???_??.txt",
"date": "Fri, 28 Dec 2018 15:53:44 GMT",
"transfer-encoding": "chunked",
"content-type": "txt/plain;charset=WINDOWS-1251"
}
I tried following option:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + filename);
and following:
response.addHeader("Content-disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(filename,"UTF-8"));
but it doesn't help
How can I fix this issue?
java spring-mvc encoding download cyrillic
java spring-mvc encoding download cyrillic
asked Dec 28 '18 at 15:58
gstackoverflowgstackoverflow
9,74445169354
9,74445169354
How did you get the headers the server sent? TheContent-Dispositionheader does not contain the*=UTF-8part
– caco3
Dec 28 '18 at 16:26
add a comment |
How did you get the headers the server sent? TheContent-Dispositionheader does not contain the*=UTF-8part
– caco3
Dec 28 '18 at 16:26
How did you get the headers the server sent? The
Content-Disposition header does not contain the *=UTF-8 part– caco3
Dec 28 '18 at 16:26
How did you get the headers the server sent? The
Content-Disposition header does not contain the *=UTF-8 part– caco3
Dec 28 '18 at 16:26
add a comment |
1 Answer
1
active
oldest
votes
If you're on Spring 5+ you can use ContentDisposition:
String filename = "русское слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
System.out.println(contentDisposition.toString());
What yields:
attachment; filename*=UTF-8''%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%B5%20%D1%81%D0%BB%D0%BE%D0%B2%D0%BE.txt
The ContentDisposition hides all the work you're trying to do (see its toString implementation):
if (this.filename != null) {
if (this.charset == null || StandardCharsets.US_ASCII.equals(this.charset)) {
sb.append("; filename="");
sb.append(this.filename).append('"');
}
else {
sb.append("; filename*=");
sb.append(encodeHeaderFieldParam(this.filename, this.charset));
}
}
Also if you don't want to deal with HttpServletRequest directly you can return ResponseEntity instead:
@RequestMapping("/")
public ResponseEntity<Resource> download() {
HttpHeaders httpHeaders = new HttpHeaders();
String filename = "русское_слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
httpHeaders.setContentDisposition(contentDisposition);
return new ResponseEntity<>(new ByteArrayResource(new byte[0]),
httpHeaders, HttpStatus.OK);
}
According the ContentDisposition class: browser suggests me filename: txt_plain_1_blob_http___localhost_8080_3be321b8-3506-4189-b63e-61377fac8a7c
– gstackoverflow
Dec 28 '18 at 20:01
Which browser are you using? Can you dump headers the server sets?
– caco3
Dec 28 '18 at 20:18
looks like it is swagger issue. Your solution works.
– gstackoverflow
Dec 28 '18 at 20:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961134%2fhow-to-return-downloaded-file-name-in-cyrillic%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you're on Spring 5+ you can use ContentDisposition:
String filename = "русское слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
System.out.println(contentDisposition.toString());
What yields:
attachment; filename*=UTF-8''%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%B5%20%D1%81%D0%BB%D0%BE%D0%B2%D0%BE.txt
The ContentDisposition hides all the work you're trying to do (see its toString implementation):
if (this.filename != null) {
if (this.charset == null || StandardCharsets.US_ASCII.equals(this.charset)) {
sb.append("; filename="");
sb.append(this.filename).append('"');
}
else {
sb.append("; filename*=");
sb.append(encodeHeaderFieldParam(this.filename, this.charset));
}
}
Also if you don't want to deal with HttpServletRequest directly you can return ResponseEntity instead:
@RequestMapping("/")
public ResponseEntity<Resource> download() {
HttpHeaders httpHeaders = new HttpHeaders();
String filename = "русское_слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
httpHeaders.setContentDisposition(contentDisposition);
return new ResponseEntity<>(new ByteArrayResource(new byte[0]),
httpHeaders, HttpStatus.OK);
}
According the ContentDisposition class: browser suggests me filename: txt_plain_1_blob_http___localhost_8080_3be321b8-3506-4189-b63e-61377fac8a7c
– gstackoverflow
Dec 28 '18 at 20:01
Which browser are you using? Can you dump headers the server sets?
– caco3
Dec 28 '18 at 20:18
looks like it is swagger issue. Your solution works.
– gstackoverflow
Dec 28 '18 at 20:28
add a comment |
If you're on Spring 5+ you can use ContentDisposition:
String filename = "русское слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
System.out.println(contentDisposition.toString());
What yields:
attachment; filename*=UTF-8''%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%B5%20%D1%81%D0%BB%D0%BE%D0%B2%D0%BE.txt
The ContentDisposition hides all the work you're trying to do (see its toString implementation):
if (this.filename != null) {
if (this.charset == null || StandardCharsets.US_ASCII.equals(this.charset)) {
sb.append("; filename="");
sb.append(this.filename).append('"');
}
else {
sb.append("; filename*=");
sb.append(encodeHeaderFieldParam(this.filename, this.charset));
}
}
Also if you don't want to deal with HttpServletRequest directly you can return ResponseEntity instead:
@RequestMapping("/")
public ResponseEntity<Resource> download() {
HttpHeaders httpHeaders = new HttpHeaders();
String filename = "русское_слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
httpHeaders.setContentDisposition(contentDisposition);
return new ResponseEntity<>(new ByteArrayResource(new byte[0]),
httpHeaders, HttpStatus.OK);
}
According the ContentDisposition class: browser suggests me filename: txt_plain_1_blob_http___localhost_8080_3be321b8-3506-4189-b63e-61377fac8a7c
– gstackoverflow
Dec 28 '18 at 20:01
Which browser are you using? Can you dump headers the server sets?
– caco3
Dec 28 '18 at 20:18
looks like it is swagger issue. Your solution works.
– gstackoverflow
Dec 28 '18 at 20:28
add a comment |
If you're on Spring 5+ you can use ContentDisposition:
String filename = "русское слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
System.out.println(contentDisposition.toString());
What yields:
attachment; filename*=UTF-8''%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%B5%20%D1%81%D0%BB%D0%BE%D0%B2%D0%BE.txt
The ContentDisposition hides all the work you're trying to do (see its toString implementation):
if (this.filename != null) {
if (this.charset == null || StandardCharsets.US_ASCII.equals(this.charset)) {
sb.append("; filename="");
sb.append(this.filename).append('"');
}
else {
sb.append("; filename*=");
sb.append(encodeHeaderFieldParam(this.filename, this.charset));
}
}
Also if you don't want to deal with HttpServletRequest directly you can return ResponseEntity instead:
@RequestMapping("/")
public ResponseEntity<Resource> download() {
HttpHeaders httpHeaders = new HttpHeaders();
String filename = "русское_слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
httpHeaders.setContentDisposition(contentDisposition);
return new ResponseEntity<>(new ByteArrayResource(new byte[0]),
httpHeaders, HttpStatus.OK);
}
If you're on Spring 5+ you can use ContentDisposition:
String filename = "русское слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
System.out.println(contentDisposition.toString());
What yields:
attachment; filename*=UTF-8''%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%B5%20%D1%81%D0%BB%D0%BE%D0%B2%D0%BE.txt
The ContentDisposition hides all the work you're trying to do (see its toString implementation):
if (this.filename != null) {
if (this.charset == null || StandardCharsets.US_ASCII.equals(this.charset)) {
sb.append("; filename="");
sb.append(this.filename).append('"');
}
else {
sb.append("; filename*=");
sb.append(encodeHeaderFieldParam(this.filename, this.charset));
}
}
Also if you don't want to deal with HttpServletRequest directly you can return ResponseEntity instead:
@RequestMapping("/")
public ResponseEntity<Resource> download() {
HttpHeaders httpHeaders = new HttpHeaders();
String filename = "русское_слово.txt";
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(filename, StandardCharsets.UTF_8)
.build();
httpHeaders.setContentDisposition(contentDisposition);
return new ResponseEntity<>(new ByteArrayResource(new byte[0]),
httpHeaders, HttpStatus.OK);
}
answered Dec 28 '18 at 16:44
caco3caco3
1,2181517
1,2181517
According the ContentDisposition class: browser suggests me filename: txt_plain_1_blob_http___localhost_8080_3be321b8-3506-4189-b63e-61377fac8a7c
– gstackoverflow
Dec 28 '18 at 20:01
Which browser are you using? Can you dump headers the server sets?
– caco3
Dec 28 '18 at 20:18
looks like it is swagger issue. Your solution works.
– gstackoverflow
Dec 28 '18 at 20:28
add a comment |
According the ContentDisposition class: browser suggests me filename: txt_plain_1_blob_http___localhost_8080_3be321b8-3506-4189-b63e-61377fac8a7c
– gstackoverflow
Dec 28 '18 at 20:01
Which browser are you using? Can you dump headers the server sets?
– caco3
Dec 28 '18 at 20:18
looks like it is swagger issue. Your solution works.
– gstackoverflow
Dec 28 '18 at 20:28
According the ContentDisposition class: browser suggests me filename: txt_plain_1_blob_http___localhost_8080_3be321b8-3506-4189-b63e-61377fac8a7c
– gstackoverflow
Dec 28 '18 at 20:01
According the ContentDisposition class: browser suggests me filename: txt_plain_1_blob_http___localhost_8080_3be321b8-3506-4189-b63e-61377fac8a7c
– gstackoverflow
Dec 28 '18 at 20:01
Which browser are you using? Can you dump headers the server sets?
– caco3
Dec 28 '18 at 20:18
Which browser are you using? Can you dump headers the server sets?
– caco3
Dec 28 '18 at 20:18
looks like it is swagger issue. Your solution works.
– gstackoverflow
Dec 28 '18 at 20:28
looks like it is swagger issue. Your solution works.
– gstackoverflow
Dec 28 '18 at 20:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961134%2fhow-to-return-downloaded-file-name-in-cyrillic%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How did you get the headers the server sent? The
Content-Dispositionheader does not contain the*=UTF-8part– caco3
Dec 28 '18 at 16:26