我必须上传一些条件的照片:
到目前为止,我发现很少或根本没有关于图片上传和/或检查Play!Framework的信息。 欢迎任何帮助!
谢谢!
答案 0 :(得分:13)
在PlayFramework的源代码中搜索了一下之后,我偶然发现了已在Play中使用的ImageIO ibrary。无法理解,为什么这些简单的检查没有被添加到核心库......
这是我创建的检查部分:
尺寸检查。
package validators;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import play.Logger;
import play.data.validation.Check;
import play.db.jpa.Blob;
import play.i18n.Messages;
public class ImageValidator extends Check {
public final static int MAX_SIZE = 4048;
public final static int MAX_HEIGHT = 1920;
@Override
public boolean isSatisfied(Object parent, Object image) {
if (!(image instanceof Blob)) {
return false;
}
if (!((Blob) image).type().equals("image/jpeg") && !((Blob) image).type().equals("image/png")) {
return false;
}
// size check
if (((Blob) image).getFile().getLength() > MAX_SIZE) {
return false;
}
try {
BufferedImage source = ImageIO.read(((Blob) image).getFile());
int width = source.getWidth();
int height = source.getHeight();
if (width > MAX_WIDTH || height > MAX_HEIGHT) {
return false;
}
} catch (IOException exption) {
return false;
}
return true;
}
}
答案 1 :(得分:1)
实施自定义检查,这是Play文档中的示例:
public class User {
@Required
@CheckWith(MyPasswordCheck.class)
public String password;
static class MyPasswordCheck extends Check {
public boolean isSatisfied(Object user, Object password) {
return notMatchPreviousPasswords(password);
}
}
}
这里有Lunatech关于使用Play上传文件的精彩帖子的链接: http://www.lunatech-research.com/playframework-file-upload-blob