【问题标题】:Play Framework, not display image after upload播放框架,上传后不显示图片
【发布时间】:2013-12-05 15:13:14
【问题描述】:

我有以下问题。我上传图像并将其保存在图像文件夹中。上传后我想显示上传的图片,但它不起作用,为什么?如果我重新启动应用程序,将显示上传的图像。为什么 ? HTML 我使用“ class="img-responsive">

图片路线:

GET /images/*file controllers.Assets.at(path="/public/images", file)

public class ImagePublicService extends Controller{

     public static Result upload() {
      MultipartFormData body = request().body().asMultipartFormData();

      FilePart picture = body.getFile("file");
      play.Logger.debug("File: "+  body);
      if (picture != null) {
        String fileName = picture.getFilename();
        String extension = fileName.substring(fileName.indexOf("."));
        String uuid = uuid=java.util.UUID.randomUUID().toString();
        fileName = uuid + extension;
        play.Logger.debug("Image: " + fileName);

        String contentType = picture.getContentType(); 
        File file = picture.getFile();
           try {
            File newFile = new File("public/images", fileName);
               FileUtils.moveFile(file,newFile );
               play.Logger.debug("File moved");
           } catch (IOException ioe) {
               System.out.println("Problem operating on filesystem");
           }
        play.Logger.debug("File uploaded");
        ObjectNode result = Json.newObject();
        result.put("src", "images/" + fileName);
        return ok(result);
       } else {
            play.Logger.debug("File not uploaded");

        flash("error", "Missing file");
        return badRequest("Fehler");   
      }
    }
}

【问题讨论】:

    标签: playframework playframework-2.0


    【解决方案1】:

    这是一个安全问题。大多数情况下,您可以在开发模式下看到上传的文件,但在生产模式下则看不到。

    解决方案是实现一个可以直接从磁盘写入/读取文件的自定义控制器。或使用将提供照片的另一个应用程序服务器,即

    有外部资产控制器,但在生产模式下被禁用 http://www.playframework.com/documentation/2.0.5/api/scala/controllers/ExternalAssets$.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多