我正在开发一个 webbapp,它从用户那里接收图像资源,但我想验证这些要上传的图像是否具有特定的尺寸,如 600X800 等。
我试图找到答案,但我没有选择:)。
如果有人以前这样做过,那就太好了。
感谢您的帮助将不胜感激
我正在开发一个 webbapp,它从用户那里接收图像资源,但我想验证这些要上传的图像是否具有特定的尺寸,如 600X800 等。
我试图找到答案,但我没有选择:)。
如果有人以前这样做过,那就太好了。
感谢您的帮助将不胜感激
尝试
BufferedImage bi = ImageIO.read (file);
bi.getWidth();//width
bi.getHeight ();//height
最后我确实找到了方法:我们首先得到它要上传的文件,然后我使用一个创建的类,我在堆栈溢出时在这里找到,找到尺寸的宽度和高度,我只是玩 if's then ,这是代码,希望对某人有所帮助!
public class AddPromo extends WebPage {
/**
*
*/
private FileUploadField fileUpload;
private String UPLOAD_FOLDER = "..\\Promociones\\";
private static final long serialVersionUID = 1L;
@SuppressWarnings({ "rawtypes", "unchecked" })
public AddPromo(){
add(new Label("user",ActiveUser.USER));
//Add Feedback panel
add(new FeedbackPanel("feedback_2"));
//LOGOUT LINK
Logout logoutLink = new Logout("logout");
add(logoutLink);
//Add backlink to SuccesPage
add(new Link("link_atras"){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void onClick(){
SuccesPage nueva = new SuccesPage("",1);
setResponsePage(nueva);
}
});
//Definitions for date attributes
final Locale selectedLocale = Session.get().getLocale();
add(new StaticImage("image_test",new Model("http://1-ps.googleusercontent.com/h/www.bizreport.com/2011/02/03/200x200xandroid-logo-200x200.jpg.pagespeed.ic.SONOBLzFc5.jpg")));
Form<?> form = new Form<Void>("form") {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit() {
JPEGDim dims = new JPEGDim();
final FileUpload uploadedFile = fileUpload.getFileUpload();
if (uploadedFile != null) {
// write to a new file
File newFile = new File(UPLOAD_FOLDER
+ uploadedFile.getClientFileName());
try {
final Dimension d = dims.getJPEGDimension(newFile);
if (d.getWidth()==1024 || d.getHeight()==768 ) {
try {
if (newFile.exists()) {
newFile.delete();
}
newFile.createNewFile();
uploadedFile.writeTo(newFile);
info("saved file: " + uploadedFile.getClientFileName());
info("Imagen cumple con dimensiones " + d.getHeight() + " x " + d.getWidth());
} catch (Exception e) {
throw new IllegalStateException("Error "+e.toString());
}
} else {
error("Archivo no valido... " + + d.getHeight() + " x " + d.getWidth());
}
} catch (IOException e1) {
// TODO Auto-generated catch block
error(e1.toString());
}
}
}
};
// Enable multipart mode (need for uploads file)
form.setMultiPart(true);
// max upload size, 10k
form.setMaxSize(Bytes.megabytes(10));
form.add(fileUpload = new FileUploadField("fileUpload"));
add(form);
}
class JPEGDim {
public Dimension getJPEGDimension(File f) throws IOException {
FileInputStream fis = new FileInputStream(f);
// check for SOI marker
if (fis.read() != 255 || fis.read() != 216)
throw new RuntimeException("SOI (Start Of Image) marker 0xff 0xd8 missing");
Dimension d = null;
while (fis.read() == 255) {
int marker = fis.read();
int len = fis.read() << 8 | fis.read();
if (marker == 192) {
fis.skip(1);
int height = fis.read() << 8 | fis.read();
int width = fis.read() << 8 | fis.read();
d = new Dimension(width, height);
break;
}
fis.skip(len - 2);
}
fis.close();
return d;
}
}
}