gpt4 book ai didi

java - 'filename' 中的 UTF-8 字符对于 'Content-Disposition' 产生 "IllegalArgumentException: Unexpected char"

转载 作者:行者123 更新时间:2023-12-02 16:00:26 27 4
gpt4 key购买 nike

是否可以从 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

此请求发送给公司合作伙伴,因此我无法访问后端。

后端需要

application/octet-stream

正文是这样创建的:

byte[] data = FileUtils.readFileToByteArray(file);
RequestBody requestBody = RequestBody.create(data);

它与 Postman 完美配合。

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

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:意外的字符 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 header 的默认字符集是 ISO-8859-1。但是有 RFC 6266 ,描述了如何在 Content-Disposition header 中对文件名进行编码。基本上,您指定字符集名称,然后对 UTF-8 字符进行百分号编码。您可以使用以 filename*=utf-8'' 开头的参数代替 fileName="my-simple-filename"

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 ,如果你想避免使用 Guava,Spring's ContentDisposition class或任何其他库,只需使用 JRE 类即可。


更新:这是一个完整的 MCVE ,展示了如何将 UTF-8 字符串作为 POST 正文和内容处置文件名发送。演示服务器展示了如何手动解码该 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

关于java - 'filename' 中的 UTF-8 字符对于 'Content-Disposition' 产生 "IllegalArgumentException: Unexpected char",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70804280/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com