0

如何使用 playframework 中的 URL 上传照片?我是这样想的:

URL url = new URL("http://www.google.ru/intl/en_com/images/logo_plain.png");
BufferedImage img = ImageIO.read(url);
File newFile = new File("google.png");
ImageIO.write(img, "png", newFile);

但也许还有另一种方式。最后我必须得到文件和文件名。

示例控制器:

public static Result uploadPhoto(String urlPhoto){ 
    Url url = new Url(urlPhoto); //doSomething 
    //get a picture and write to a temporary file
    File tempPhoto = myUploadPhoto;
    uploadFile(tempPhoto); // Here we make a copy of the file and save it to the file system.
    return ok('something');
}
4

2 回答 2

2

要获取该照片,您可以使用 Play WS API,后面的代码是从处理大型响应部分中的播放文档中提取的示例,我建议您在此处阅读完整文档

final Promise<File> filePromise = WS.url(url).get().map(
        new Function<WSResponse, File>() {
            public File apply(WSResponse response) throws Throwable {

                InputStream inputStream = null;
                OutputStream outputStream = null;
                try {
                    inputStream = response.getBodyAsStream();

                    // write the inputStream to a File
                    final File file = new File("/tmp/response.txt");
                    outputStream = new FileOutputStream(file);

                    int read = 0;
                    byte[] buffer = new byte[1024];

                    while ((read = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, read);
                    }

                    return file;
                } catch (IOException e) {
                    throw e;
                } finally {
                    if (inputStream != null) {inputStream.close();}
                    if (outputStream != null) {outputStream.close();}
                }

            }
        }
);

网址在哪里:

String url = "http://www.google.ru/intl/en_com/images/logo_plain.png"

正如大文件的播放文档中所建议的那样:

*

当您下载大型文件或文档时,WS 允许您将响应主体作为 InputStream 获取,这样您就可以处理数据而无需一次将整个内容加载到内存中。

*

于 2015-04-16T12:46:11.867 回答
1

与上面的答案几乎相同,然后一些......

路线:POST /testFile '你的控制器的位置在这里'

请求正文内容:{"url":"http://www.google.ru/intl/en_com/images/logo_plain.png"}

控制器(使用JavaWS 处理大型响应的代码):

public static Promise<Result> saveFile() {
    //you send the url in the request body in order to avoid complications with encoding
    final JsonNode body = request().body().asJson();
    // use new URL() to validate... not including it for brevity
    final String url = body.get("url").asText();
    //this one's copy/paste from Play Framework's docs 
    final Promise<File> filePromise = WS.url(url).get().map(response -> {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = response.getBodyAsStream();
            final File file = new File("/temp/image");
            outputStream = new FileOutputStream(file);
            int read = 0;
            byte[] buffer = new byte[1024];
            while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
            }
            return file;
        } catch (IOException e) {
            throw e;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }); // copy/paste ended
    return filePromise.map(file -> (Result) ok(file.getName() + " saved!")).recover(
            t -> (Result) internalServerError("error -> " + t.getMessage()));
}

就是这样……

为了在上传阶段之后提供文件,您可以使用这个答案(我发誓我不是在推销自己......):从游戏框架 2.3.x 中的绝对路径提供静态资产服务

于 2015-04-16T15:58:23.773 回答