【发布时间】: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