2017-09-15 1 views
1

Ich bin derzeit versucht, eine Datei zu einem Endpunkt posten die WSClient verwenden, mit dem folgenden CodePosting MultipartFormData mit dem 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(); 
    } 

} 

Ich bin mit der Framework-Version 2.5.15 und mit Java 8 . Das Problem, das ich bin immer ist

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

ich bin nicht sicher, wie dieses Problem zu lösen. Wenn mir jemand in die richtige Richtung zeigen könnte, wäre das sehr geschätzt. Danke

Antwort

1

Ich bin mir nicht sicher, aber mit Blick auf den Stack-Trace denke ich, dass Sie möglicherweise den falschen WSClient importiert haben, den von Scala anstelle von dem von Java.

In der Regel ist alles mit api wie hier in play.api.libs.ws.WSRequest.<T>post Scala Zeug. Ändern Sie Ihren Import, es könnte Ihr Problem lösen.

+0

Das war in der Tat mein Problem, danke für Ihre Hilfe. – Ergun

+0

Gern geschehen! kannst du die antwort einfach annehmen;)? –