gpt4 book ai didi

java - 使用 java 通过 WSClient 发布 MultipartFormData

转载 作者:行者123 更新时间:2023-11-30 02:23:31 24 4
gpt4 key购买 nike

我当前正在尝试使用 WSClient 使用以下代码将文件发布到端点

public Result uploadBankingFile(){
logger.info("Uploading file to cold fusion");
MultipartFormData<File> body = request().body().asMultipartFormData();
MultipartFormData.FilePart<File> bankingFile = body.getFile("bankingFile");

if (bankingFile != null) {
String fileName = bankingFile.getFilename();
String contentType = bankingFile.getContentType();

//field needs to be called import
Source<ByteString, ?> file = FileIO.fromFile(bankingFile.getFile());
MultipartFormData.FilePart<Source<ByteString, ?>> fp = new MultipartFormData.FilePart<>("import", fileName, "text/plain", file);
MultipartFormData.DataPart dp = new MultipartFormData.DataPart("key", "value");

Future<WSResponse> post = ws.url(coldFusionPath + coldFusionUploadPath).post(Source.from(Arrays.asList(fp,dp)));


return new JsonResult("ok");
} else {
flash("error", "Missing file");
return badRequest();
}

}

我正在使用框架版本 2.5.15 和 java 8。我遇到的问题是

/ImportBankingData.java:58: no suitable method found for post(akka.stream.javadsl.Source<play.mvc.Http.MultipartFormData.Part<akka.stream.javadsl.Source<akka.util.ByteString,?>>,akka.NotUsed>)
[error] method play.api.libs.ws.WSRequest.<T>post(T,play.api.http.Writeable<T>) is not applicable
[error] (cannot infer type-variable(s) T
[error] (actual and formal argument lists differ in length))
[error] method play.api.libs.ws.WSRequest.post(java.io.File) is not applicable
[error] (argument mismatch; no instance(s) of type variable(s) O,T exist so that akka.stream.javadsl.Source<O,akka.NotUsed> conforms to java.io.File)
[error] method play.api.libs.ws.WSRequest.post(akka.stream.scaladsl.Source<play.api.mvc.MultipartFormData.Part<akka.stream.scaladsl.Source<akka.util.ByteString,?>>,?>) is not applicable
[error] (argument mismatch; no instance(s) of type variable(s) O,T exist so that akka.stream.javadsl.Source<O,akka.NotUsed> conforms to akka.stream.scaladsl.Source<play.api.mvc.MultipartFormData.Part<akka.stream.scaladsl.Source<akka.util.ByteString,?>>,?>)
[error] ws.url(coldFusionPath + coldFusionUploadPath).post
[error] (compile:compileIncremental) javac returned nonzero exit code
[info] Compiling 1 Java source to /Users/ergun/Documents/projects/brightbook/web/target/scala-2.11/classes...
[error] /Users/ergun/Documents/projects/brightbook/web/app/co/brightbook/web/controllers/ImportBankingData.java:58: no suitable method found for post(akka.stream.javadsl.Source<play.mvc.Http.MultipartFormData.Part<akka.stream.javadsl.Source<akka.util.ByteString,?>>,akka.NotUsed>)
[error] method play.api.libs.ws.WSRequest.<T>post(T,play.api.http.Writeable<T>) is not applicable
[error] (cannot infer type-variable(s) T
[error] (actual and formal argument lists differ in length))
[error] method play.api.libs.ws.WSRequest.post(java.io.File) is not applicable
[error] (argument mismatch; no instance(s) of type variable(s) O,T exist so that akka.stream.javadsl.Source<O,akka.NotUsed> conforms to java.io.File)
[error] method play.api.libs.ws.WSRequest.post(akka.stream.scaladsl.Source<play.api.mvc.MultipartFormData.Part<akka.stream.scaladsl.Source<akka.util.ByteString,?>>,?>) is not applicable
[error] (argument mismatch; no instance(s) of type variable(s) O,T exist so that akka.stream.javadsl.Source<O,akka.NotUsed> conforms to akka.stream.scaladsl.Source<play.api.mvc.MultipartFormData.Part<akka.stream.scaladsl.Source<akka.util.ByteString,?>>,?>)
[error] ws.url(coldFusionPath + coldFusionUploadPath).post
[error] (compile:compileIncremental) javac returned nonzero exit code
[error] application -

我不知道如何解决这个问题。如果有人能指出我正确的方向,我将不胜感激。谢谢

最佳答案

我不确定,但是查看堆栈跟踪,我认为您可能导入了错误的 WSClient,即来自 Scala 的而不是来自 Java 的 WSClient。

一般来说,所有带有 api 的内容就像这里的play.api.libs.ws.WSRequest.<T>post是Scala的东西。更改您的导入,它可能会解决您的问题。

关于java - 使用 java 通过 WSClient 发布 MultipartFormData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46235599/

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