首页 文章

Play Framework,上传后不显示图片

提问于
浏览
1

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

图片路线:

GET / images / *文件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");   
      }
    }
}

1 回答

  • 0

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

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

    有外部资产控制器,但在 生产环境 模式http://www.playframework.com/documentation/2.0.5/api/scala/controllers/ExternalAssets $ .html中禁用此功能

相关问题