'filename' 中的 UTF-8 字符对于 'Content-Disposition' 产生 "IllegalArgumentException: Unexpected char"
UTF-8 characters in 'filename' for 'Content-Disposition' yield "IllegalArgumentException: Unexpected char"
是否可以从 okhttp3 客户端发送 UTF-8 字符?
对于以下字符串:
String fileName = "3$ Mù F'RANçé_33902_Country_5_202105";
String contentDisposition = "attachment;filename=" + "\"" + fileName + "\"";
我试过了(contentDisposition header):
Headers headers = new Headers.Builder()
.addUnsafeNonAscii("Content-Disposition", contentDisposition)
.add("Authorization", bearer)
.add("Content-type", "application/octet-stream")
.build();
Request request = new Request.Builder()
.headers(headers)
.post(requestBody)
.url(urlAddress)
.build();
但是服务器收到:3$ Mù F'RANçé_33902_Country_5_202105
此请求发送给公司合作伙伴,因此我无法访问 back-end。
application/octet-stream
是 back-end 所需要的。
Body 是这样创建的:
byte[] data = FileUtils.readFileToByteArray(file);
RequestBody requestBody = RequestBody.create(data);
它与 Postman 完美配合。
完整的 MVCE(无法完成文件和 back-end 信息,但它之前崩溃了,无论如何,所以你可以直接启动这个确切的代码,它应该会抛出错误):
public class App
{
public static void main( String[] args ) throws IOException
{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("xxxx")
.method("POST", body)
.addHeader("Content-Type", "application/octet-stream")
.addHeader("content-disposition", "attachment;filename=\"3$ Mù F'RANçé_33902_Country_5_202105.csv\"")
.addHeader("Authorization", "Bearer xxxxx")
.addHeader("Cookie", "xxxxxx")
.build();
Response response = client.newCall(request).execute();
}
}
收到错误:java.lang.IllegalArgumentException: Unexpected char 0xf9 at 25 in content-disposition value: attachment;filename="3$ Mù F'RANçé_33902_Country_5_202105.csv"
okhttp 版本:5.0.0-alpha.2
我是不是漏掉了什么?
谢谢
HTTP headers 的默认字符集是 ISO-8859-1。但是有 RFC 6266,描述了如何在 Content-Disposition
header 中编码文件名。基本上,您指定字符集名称,然后 percent-encode UTF-8 字符。而不是 fileName="my-simple-filename"
你使用以 filename*=utf-8''
开头的参数 like
import java.net.URLEncoder;
// ...
String fileName = "3$ Mù F'RANçé_33902_Country_5_202105";
String contentDisposition = "attachment;filename*=utf-8''" + encodeFileName(fileName);
// ...
private static String encodeFileName(String fileName) throws UnsupportedEncodingException {
return URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
}
使用 URL 编码器然后修改“+”的结果是我发现 here, if you want to avoid using Guava, Spring's ContentDisposition
class 或任何其他库的廉价技巧,只需使用 JRE 类.
更新: 这是完整的 MCVE,显示了如何将 UTF-8 字符串作为 POST body 和作为内容处置文件名。演示服务器展示了如何手动解码 header——通常 HTTP 服务器应该自动解码。
Maven POM 显示使用的依赖项:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SO_Java_OkHttp3SendUtf8_70804280</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<dependency>
<groupId>org.nanohttpd</groupId>
<artifactId>nanohttpd</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>
OkHttp 演示客户端:
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
public class Client {
public static void main(String[] args) throws IOException {
String fileName = "3$ Mù F'RANçé_33902_Country_5_202105";
String contentDisposition = "attachment;filename*=utf-8''" + encodeFileName(fileName);
RequestBody requestBody = RequestBody.create(fileName.getBytes(StandardCharsets.UTF_8));
Headers headers = new Headers.Builder()
.add("Content-Disposition", contentDisposition)
.add("Content-type", "application/octet-stream; charset=utf-8")
.build();
Request request = new Request.Builder()
.headers(headers)
.post(requestBody)
.url(new URL("http://localhost:8080/"))
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
System.out.println(Objects.requireNonNull(response.body()).string());
}
private static String encodeFileName(String fileName) {
return URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");
}
}
NanoHTTPD 演示服务器:
import fi.iki.elonen.NanoHTTPD;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class Server extends NanoHTTPD {
public Server() throws IOException {
super(8080);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
}
public static void main(String[] args) throws IOException {
new Server();
}
private static final String UTF_8_FILE_NAME_PREFIX = ";filename*=utf-8''";
private static final int UTF_8_FILE_NAME_PREFIX_LENGTH = UTF_8_FILE_NAME_PREFIX.length();
@Override
public Response serve(IHTTPSession session) {
try {
Map<String, String> files = new HashMap<>();
session.parseBody(files);
String postBody = files.get("postData");
String contentDisposition = session.getHeaders().get("content-disposition");
String fileName = decodeFileName(
contentDisposition.substring(
contentDisposition.indexOf(UTF_8_FILE_NAME_PREFIX) + UTF_8_FILE_NAME_PREFIX_LENGTH
)
);
System.out.println("POST body: " + postBody);
System.out.println("Content disposition: " + contentDisposition);
System.out.println("UTF-8 file name: " + fileName);
return newFixedLengthResponse(postBody + "\n" + fileName);
}
catch (IOException | ResponseException e) {
e.printStackTrace();
return newFixedLengthResponse(e.toString());
}
}
private static String decodeFileName(String fileName) {
return URLDecoder.decode(fileName.replace("%20", "+"), StandardCharsets.UTF_8);
}
}
如果您先 运行 服务器然后是客户端,您将在服务器控制台上看到:
Running! Point your browsers to http://localhost:8080/
POST body: 3$ Mù F'RANçé_33902_Country_5_202105
Content disposition: attachment;filename*=utf-8''3%24%20M%C3%B9%20F%27RAN%C3%A7%C3%A9_33902_Country_5_202105
UTF-8 file name: 3$ Mù F'RANçé_33902_Country_5_202105
在客户端控制台上,您会看到:
3$ Mù F'RANçé_33902_Country_5_202105
3$ Mù F'RANçé_33902_Country_5_202105
是否可以从 okhttp3 客户端发送 UTF-8 字符?
对于以下字符串:
String fileName = "3$ Mù F'RANçé_33902_Country_5_202105";
String contentDisposition = "attachment;filename=" + "\"" + fileName + "\"";
我试过了(contentDisposition header):
Headers headers = new Headers.Builder()
.addUnsafeNonAscii("Content-Disposition", contentDisposition)
.add("Authorization", bearer)
.add("Content-type", "application/octet-stream")
.build();
Request request = new Request.Builder()
.headers(headers)
.post(requestBody)
.url(urlAddress)
.build();
但是服务器收到:3$ Mù F'RANçé_33902_Country_5_202105
此请求发送给公司合作伙伴,因此我无法访问 back-end。
application/octet-stream
是 back-end 所需要的。
Body 是这样创建的:
byte[] data = FileUtils.readFileToByteArray(file);
RequestBody requestBody = RequestBody.create(data);
它与 Postman 完美配合。
完整的 MVCE(无法完成文件和 back-end 信息,但它之前崩溃了,无论如何,所以你可以直接启动这个确切的代码,它应该会抛出错误):
public class App
{
public static void main( String[] args ) throws IOException
{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("xxxx")
.method("POST", body)
.addHeader("Content-Type", "application/octet-stream")
.addHeader("content-disposition", "attachment;filename=\"3$ Mù F'RANçé_33902_Country_5_202105.csv\"")
.addHeader("Authorization", "Bearer xxxxx")
.addHeader("Cookie", "xxxxxx")
.build();
Response response = client.newCall(request).execute();
}
}
收到错误:java.lang.IllegalArgumentException: Unexpected char 0xf9 at 25 in content-disposition value: attachment;filename="3$ Mù F'RANçé_33902_Country_5_202105.csv"
okhttp 版本:5.0.0-alpha.2
我是不是漏掉了什么?
谢谢
HTTP headers 的默认字符集是 ISO-8859-1。但是有 RFC 6266,描述了如何在 Content-Disposition
header 中编码文件名。基本上,您指定字符集名称,然后 percent-encode UTF-8 字符。而不是 fileName="my-simple-filename"
你使用以 filename*=utf-8''
开头的参数 like
import java.net.URLEncoder;
// ...
String fileName = "3$ Mù F'RANçé_33902_Country_5_202105";
String contentDisposition = "attachment;filename*=utf-8''" + encodeFileName(fileName);
// ...
private static String encodeFileName(String fileName) throws UnsupportedEncodingException {
return URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
}
使用 URL 编码器然后修改“+”的结果是我发现 here, if you want to avoid using Guava, Spring's ContentDisposition
class 或任何其他库的廉价技巧,只需使用 JRE 类.
更新: 这是完整的 MCVE,显示了如何将 UTF-8 字符串作为 POST body 和作为内容处置文件名。演示服务器展示了如何手动解码 header——通常 HTTP 服务器应该自动解码。
Maven POM 显示使用的依赖项:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SO_Java_OkHttp3SendUtf8_70804280</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<dependency>
<groupId>org.nanohttpd</groupId>
<artifactId>nanohttpd</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>
OkHttp 演示客户端:
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
public class Client {
public static void main(String[] args) throws IOException {
String fileName = "3$ Mù F'RANçé_33902_Country_5_202105";
String contentDisposition = "attachment;filename*=utf-8''" + encodeFileName(fileName);
RequestBody requestBody = RequestBody.create(fileName.getBytes(StandardCharsets.UTF_8));
Headers headers = new Headers.Builder()
.add("Content-Disposition", contentDisposition)
.add("Content-type", "application/octet-stream; charset=utf-8")
.build();
Request request = new Request.Builder()
.headers(headers)
.post(requestBody)
.url(new URL("http://localhost:8080/"))
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
System.out.println(Objects.requireNonNull(response.body()).string());
}
private static String encodeFileName(String fileName) {
return URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");
}
}
NanoHTTPD 演示服务器:
import fi.iki.elonen.NanoHTTPD;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class Server extends NanoHTTPD {
public Server() throws IOException {
super(8080);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
}
public static void main(String[] args) throws IOException {
new Server();
}
private static final String UTF_8_FILE_NAME_PREFIX = ";filename*=utf-8''";
private static final int UTF_8_FILE_NAME_PREFIX_LENGTH = UTF_8_FILE_NAME_PREFIX.length();
@Override
public Response serve(IHTTPSession session) {
try {
Map<String, String> files = new HashMap<>();
session.parseBody(files);
String postBody = files.get("postData");
String contentDisposition = session.getHeaders().get("content-disposition");
String fileName = decodeFileName(
contentDisposition.substring(
contentDisposition.indexOf(UTF_8_FILE_NAME_PREFIX) + UTF_8_FILE_NAME_PREFIX_LENGTH
)
);
System.out.println("POST body: " + postBody);
System.out.println("Content disposition: " + contentDisposition);
System.out.println("UTF-8 file name: " + fileName);
return newFixedLengthResponse(postBody + "\n" + fileName);
}
catch (IOException | ResponseException e) {
e.printStackTrace();
return newFixedLengthResponse(e.toString());
}
}
private static String decodeFileName(String fileName) {
return URLDecoder.decode(fileName.replace("%20", "+"), StandardCharsets.UTF_8);
}
}
如果您先 运行 服务器然后是客户端,您将在服务器控制台上看到:
Running! Point your browsers to http://localhost:8080/
POST body: 3$ Mù F'RANçé_33902_Country_5_202105
Content disposition: attachment;filename*=utf-8''3%24%20M%C3%B9%20F%27RAN%C3%A7%C3%A9_33902_Country_5_202105
UTF-8 file name: 3$ Mù F'RANçé_33902_Country_5_202105
在客户端控制台上,您会看到:
3$ Mù F'RANçé_33902_Country_5_202105
3$ Mù F'RANçé_33902_Country_5_202105