2017-09-15 67 views
1

我目前正在使用WSClient发布文件到终点,用下面的代码用java

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发布MultipartFormData与WSClient 。我得到的问题是

/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 - 

我不知道如何解决此问题。如果任何人都可以指出我正确的方向,将不胜感激。谢谢

回答

1

我不确定,但看着堆栈跟踪我想你可能已经导入了错误的WSClient,一个来自Scala而不是来自Java的WSClient。

一般来说,所有与api像这里在play.api.libs.ws.WSRequest.<T>post是斯卡拉的东西。改变你的导入,它可能会解决你的问题。

+0

这确实是我的问题,谢谢你的帮助。 – Ergun

+0

不客气!你能接受答案吗?)? –