gpt4 book ai didi

java - 如何修改java Rest模板客户端中的部分Content-Disposition

转载 作者:行者123 更新时间:2023-12-02 02:04:15 35 4
gpt4 key购买 nike

我正在努力使用 java spring 创建对某些内部服务的有效请求。问题在于多部分/表单数据边界的正确负载。

环境:java 服务器 -> (其余)http multipart/form-data -> 一些服务

(中间没有浏览器)

有效负载应如下所示:

------WebKitFormBoundaryp8mrQWOb5GiyC90yContent-Disposition: form-data; name="files"; filename="0000.png"Content-Type: image/png[binary data]------WebKitFormBoundaryp8mrQWOb5GiyC90y--

Unfortunately I'm unable to change this "headers" and I'm getting something like this:

 --fkGT7CJaQB9-2aa8G1ePv17iHKnWSsdContent-Disposition: form-data; name="files"Content-Length: 170096[binary data]--fkGT7CJaQB9-2aa8G1ePv17iHKnWSsd--

I've searched many stackoverlow questions, but nothing seems to work.This is what I've done till now (generates above payload):

HashMap<String, List<String>> additionalHeaders = new HashMap<>();
String fileMd5 = "tgrlfG0pjblWZB6g1f7j5w=="; //@todo
File file = new File(systemFile.getAbsoluteFileLocation());
Path filePath = Paths.get(systemFile.getAbsoluteFileLocation());
try{
DiskFileItem fileItem = new DiskFileItem("file", "image/png", false, file.getName(), (int) file.length() , file.getParentFile());
InputStream input = new FileInputStream(file);
OutputStream os = fileItem.getOutputStream();
int ret = input.read();
while ( ret != -1 )
{
os.write(ret);
ret = input.read();
}
os.flush();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);


MultiValueMap<String, Object> parts =
new LinkedMultiValueMap<>();
ByteArrayResource resource = new ByteArrayResource(multipartFile.getBytes());
parts.add("files", resource);


additionalHeaders.put("Content-MD5", Collections.singletonList(fileMd5));
additionalHeaders.put("Content-Disposition", Collections.singletonList("attachment; filename=\""+systemFile.getFilenameWithExtension()+"\""));
ResponseEntity<FrpFileServer> responseEntity = formDataRestClient.post(this, parts, FrpFileServer.class, isServerSide, frpToken.getTokenId(), additionalHeaders, MediaType.MULTIPART_FORM_DATA);
return responseEntity.getBody();
} catch (IOException e) {
return null;
}

formDataRestClient 通过 RestTemplate 构建请求的其余部分

public <K, T> ResponseEntity<T> post(RestClientInterface reference, K requestClass, Class<T> responseClass, boolean isServerSide, String resourceId, HashMap<String, List<String>> additionalHeaders, MediaType contentType) {

最佳答案

好的,我已经解决了用以下内容替换资源创建的问题:

ByteArrayResource resource = new ByteArrayResource(multipartFile.getBytes()){
@Override
public String getFilename() {
return systemFile.getFilenameWithExtension();
}
};

感谢这一点,restTemplate 以正确的方式处理它的魔力:)

关于java - 如何修改java Rest模板客户端中的部分Content-Disposition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045892/

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