1
votes

I have the following problem. I upload an image an save it in images folder. After upload i would like to display the uploaded image, but it not work why? If I restart the application the uploaded images would display. Why ? HTML I use " class="img-responsive">

Image Route:

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");   
      }
    }
}
1

1 Answers

0
votes

It's a security issue. You can see the uploaded file in most cases while in development mode but in production mode you can't.

Te solution is to implement an custom controller that can write/read files directly from disk. Or use anoter application server that will serve the photos i.e.

There is the external assets controller but this is disabled in production mode http://www.playframework.com/documentation/2.0.5/api/scala/controllers/ExternalAssets$.html