“Content-Disposition”的“文件名”中的 UTF-8 字符产生“IllegalArgumentException:意外的字符”

Ces*_*sar 5 java http-headers okhttp

是否可以从 okhttp3 客户端发送 UTF-8 字符?

\n

对于以下字符串:

\n
String fileName = "3$ M\xc3\xb9 F'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105";\nString contentDisposition = "attachment;filename=" + "\\"" +  fileName + "\\"";\n
Run Code Online (Sandbox Code Playgroud)\n

我已经尝试过(对于 contentDisposition 标头):

\n
Headers headers = new Headers.Builder()\n                       .addUnsafeNonAscii("Content-Disposition", contentDisposition)\n                       .add("Authorization", bearer)\n                       .add("Content-type", "application/octet-stream")\n                       .build();\n             Request request = new Request.Builder()\n                     .headers(headers)\n                     .post(requestBody) \n                     .url(urlAddress)\n                     .build();\n
Run Code Online (Sandbox Code Playgroud)\n

但服务器收到:3$ M\xc3\x83\xc2\xb9 F'RAN\xc3\x83\xc2\xa7\xc3\x83\xc2\xa9_33902_Country_5_202105

\n

该请求发送给一个固定的合作伙伴,因此我无法访问后端。

\n

application/octet-stream是后端需要的。

\n

身体是这样创建的:

\n
byte[] data = FileUtils.readFileToByteArray(file);\nRequestBody requestBody = RequestBody.create(data);\n
Run Code Online (Sandbox Code Playgroud)\n

它与 Postman 完美配合。

\n

完整的 MVCE(无法包含文件和后端信息,但无论如何它之前都会崩溃,所以您可以启动这个确切的代码,它应该会抛出错误):

\n
public class App \n{\n    public static void main( String[] args ) throws IOException\n    {\n                OkHttpClient client = new OkHttpClient().newBuilder()\n                    .build();\n                MediaType mediaType = MediaType.parse("application/octet-stream");\n                RequestBody body = RequestBody.create(mediaType, "");\n                Request request = new Request.Builder()\n                  .url("xxxx")\n                  .method("POST", body)\n                  .addHeader("Content-Type", "application/octet-stream")\n                  .addHeader("content-disposition", "attachment;filename=\\"3$ M\xc3\xb9 F'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105.csv\\"")\n                  .addHeader("Authorization", "Bearer xxxxx")\n                  .addHeader("Cookie", "xxxxxx")\n                  .build();\n                Response response = client.newCall(request).execute();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

收到错误:java.lang.IllegalArgumentException: Unexpected char 0xf9 at 25 in content-disposition value: attachment;filename="3$ M\xc3\xb9 F'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105.csv"

\n

好的http版本:5.0.0-alpha.2

\n

我错过了什么 ?

\n

谢谢

\n

kri*_*aex 15

HTTP 标头的默认字符集是 ISO-8859-1。然而, RFC 6266描述了如何在标头中对文件名进行编码Content-Disposition。基本上,您指定字符集名称,然后对 UTF-8 字符进行百分比编码。而不是使用以likefileName="my-simple-filename"开头的参数filename*=utf-8\'\'

\n
import java.net.URLEncoder;\n\n// ...\n\nString fileName = "3$ M\xc3\xb9 F\'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105";\nString contentDisposition = "attachment;filename*=utf-8\'\'" + encodeFileName(fileName);\n\n// ...\n\nprivate static String encodeFileName(String fileName) throws UnsupportedEncodingException {\n  return URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");\n}\n
Run Code Online (Sandbox Code Playgroud)\n

如果您想避免使用 Guava、Spring或任何其他库并仅使用 JRE 类,那么使用 URL 编码器然后修改“+”的结果是我在这里发现的一个廉价技巧。ContentDisposition

\n
\n

更新:这是完整的MCVE,展示了如何将 UTF-8 字符串作为 POST 正文和内容处置文件名发送。演示服务器展示了如何手动解码该标头 - 通常 HTTP 服务器应该自动执行此操作。

\n

显示使用的依赖项的 Maven POM:

\n
<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\n  <modelVersion>4.0.0</modelVersion>\n\n  <groupId>org.example</groupId>\n  <artifactId>SO_Java_OkHttp3SendUtf8_70804280</artifactId>\n  <version>1.0-SNAPSHOT</version>\n\n  <properties>\n    <maven.compiler.source>11</maven.compiler.source>\n    <maven.compiler.target>11</maven.compiler.target>\n  </properties>\n\n  <dependencies>\n    <dependency>\n      <groupId>com.squareup.okhttp3</groupId>\n      <artifactId>okhttp</artifactId>\n      <version>4.9.3</version>\n    </dependency>\n    <dependency>\n      <groupId>org.nanohttpd</groupId>\n      <artifactId>nanohttpd</artifactId>\n      <version>2.3.1</version>\n    </dependency>\n  </dependencies>\n\n</project>\n
Run Code Online (Sandbox Code Playgroud)\n

OkHttp 演示客户端:

\n
import okhttp3.Headers;\nimport okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.RequestBody;\nimport okhttp3.Response;\n\nimport java.io.IOException;\nimport java.net.URL;\nimport java.net.URLEncoder;\nimport java.nio.charset.StandardCharsets;\nimport java.util.Objects;\n\npublic class Client {\n  public static void main(String[] args) throws IOException {\n    String fileName = "3$ M\xc3\xb9 F\'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105";\n    String contentDisposition = "attachment;filename*=utf-8\'\'" + encodeFileName(fileName);\n    RequestBody requestBody = RequestBody.create(fileName.getBytes(StandardCharsets.UTF_8));\n    Headers headers = new Headers.Builder()\n      .add("Content-Disposition", contentDisposition)\n      .add("Content-type", "application/octet-stream; charset=utf-8")\n      .build();\n    Request request = new Request.Builder()\n      .headers(headers)\n      .post(requestBody)\n      .url(new URL("http://localhost:8080/"))\n      .build();\n    OkHttpClient client = new OkHttpClient();\n    Response response = client.newCall(request).execute();\n    System.out.println(Objects.requireNonNull(response.body()).string());\n  }\n\n  private static String encodeFileName(String fileName) {\n    return URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

NanoHTTPD 演示服务器:

\n
import fi.iki.elonen.NanoHTTPD;\n\nimport java.io.IOException;\nimport java.net.URLDecoder;\nimport java.nio.charset.StandardCharsets;\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class Server extends NanoHTTPD {\n\n  public Server() throws IOException {\n    super(8080);\n    start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);\n    System.out.println("\\nRunning! Point your browsers to http://localhost:8080/ \\n");\n  }\n\n  public static void main(String[] args) throws IOException {\n    new Server();\n  }\n\n  private static final String UTF_8_FILE_NAME_PREFIX = ";filename*=utf-8\'\'";\n  private static final int UTF_8_FILE_NAME_PREFIX_LENGTH = UTF_8_FILE_NAME_PREFIX.length();\n\n  @Override\n  public Response serve(IHTTPSession session) {\n    try {\n      Map<String, String> files = new HashMap<>();\n      session.parseBody(files);\n      String postBody = files.get("postData");\n      String contentDisposition = session.getHeaders().get("content-disposition");\n      String fileName = decodeFileName(\n        contentDisposition.substring(\n          contentDisposition.indexOf(UTF_8_FILE_NAME_PREFIX) + UTF_8_FILE_NAME_PREFIX_LENGTH\n        )\n      );\n      System.out.println("POST body:           " + postBody);\n      System.out.println("Content disposition: " + contentDisposition);\n      System.out.println("UTF-8 file name:     " + fileName);\n      return newFixedLengthResponse(postBody + "\\n" + fileName);\n    }\n    catch (IOException | ResponseException e) {\n      e.printStackTrace();\n      return newFixedLengthResponse(e.toString());\n    }\n  }\n\n  private static String decodeFileName(String fileName) {\n    return URLDecoder.decode(fileName.replace("%20", "+"), StandardCharsets.UTF_8);\n  }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

如果首先运行服务器,然后运行客户端,您将在服务器控制台上看到以下内容:

\n
Running! Point your browsers to http://localhost:8080/ \n\nPOST body:           3$ M\xc3\xb9 F\'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105\nContent disposition: attachment;filename*=utf-8\'\'3%24%20M%C3%B9%20F%27RAN%C3%A7%C3%A9_33902_Country_5_202105\nUTF-8 file name:     3$ M\xc3\xb9 F\'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105\n
Run Code Online (Sandbox Code Playgroud)\n

在客户端控制台上,您会看到:

\n
3$ M\xc3\xb9 F\'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105\n3$ M\xc3\xb9 F\'RAN\xc3\xa7\xc3\xa9_33902_Country_5_202105\n
Run Code Online (Sandbox Code Playgroud)\n