使用java

时间:2017-09-15 08:56:24

标签: java playframework

我目前正在尝试使用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 - 

我不确定如何解决此问题。如果有人能指出我正确的方向,将非常感激。谢谢

2 个答案:

答案 0 :(得分:1)

我不确定,但是看一下堆栈跟踪我认为你可能导入了错误的WSClient,一个来自Scala而不是来自Java的那个。

一般情况下,apiplay.api.libs.ws.WSRequest.<T>post的所有内容都是Scala的内容。更改导入,可能会解决您的问题。

答案 1 :(得分:0)

我在最新的播放版本中尝试了以下方法,并且对我有用。 我创建了一个方法getMultipartBody,该方法以wsClient调用后所需的格式返回对象,并以当前请求中的multiPartData作为输入。

public CompletionStage<Result> thirdPartyAttachment(){
    Http.MultipartFormData multipartFormData = request().body().asMultipartFormData();
    Source<? super Http.MultipartFormData.Part<Source<ByteString, ?>>, ?> multipartBody = getMultipartBody(multipartFormData.asFormUrlEncoded(),multipartFormData.getFiles());

    String url = "url_to_call";
    return wsClient.url(url)
            .post(multipartBody)
            .thenApplyAsync(response -> {
                new JsonResult("ok");
            })
            .exceptionally(e -> {
                return Results.internalServerError(prepareError());
            });
}

private Source<? super Http.MultipartFormData.Part<Source<ByteString, ?>>, ?> getMultipartBody(Map<String, String[]> dataSet, List<Http.MultipartFormData.FilePart<File>> filePartsList) {
        List<Http.MultipartFormData.Part> partList = new ArrayList<>();
        if (dataSet != null) {
            for (String field : dataSet.keySet()) {
                Http.MultipartFormData.DataPart dataPart = new Http.MultipartFormData.DataPart(field, dataSet.get(field)[0]);
                partList.add(dataPart);
            }
        }
        if (filePartsList != null) {
            for(Http.MultipartFormData.FilePart<File> filePart:filePartsList){
                Source<ByteString, ?> file = FileIO.fromFile(filePart.getFile());
                Http.MultipartFormData.FilePart<Source<ByteString, ?>> fp = new Http.MultipartFormData.FilePart<>(filePart.getKey(), filePart.getFilename(), filePart.getContentType(), file);
                partList.add(fp);
            }
        }
        return Source.from(Collections.unmodifiableList(partList));
    }