如果未附加图像,则限制文件上载

时间:2016-03-05 19:03:15

标签: jsf file-upload primefaces javabeans multipartform-data

我有一个jsf表单,其中包含用户的电子邮件地址,简要说明和图像。当file == null时,完全忽略下面的else语句。如果没有上传图片,我想要告诉用户他们必须上传图片时出错。我该如何做到这一点?

import org.primefaces.model.UploadedFile;

 private UploadedFile file;

   public UploadedFile getFile() {
        return file;
   }




   public void setFile(UploadedFile file) {
        this.file = file;
   }

  public String upload() {


    if (file!=null ) {
        try {
            System.out.println(file.getFileName());
            InputStream fin2 = file.getInputstream();
            Connection con = DataConnect.getConnection();
            PreparedStatement pre = con.prepareStatement("insert into     PICTURE_TABLE (EMAIL_ADDRESS,PRICE,ITEM_DESC,PICTURES,DATE) values(?,?,?,?,?)");
            pre.setString(1, email);

            pre.setString(2, price);
            pre.setString(3,itemDesc);

            pre.setBinaryStream(4, fin2, file.getSize());
            pre.setString(5,time);
            pre.executeUpdate();
            System.out.println("Inserting Successfully!");




       }


        catch (Exception e) {
            System.out.println("Exception-File Upload." + e.getMessage());
         return "imageFail";

        }
    }
    else{
    FacesMessage msg = new FacesMessage("Please select image!!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
    }

这是我正在使用的jsf标签。

<h:form enctype="multipart/form-data" 

0 个答案:

没有答案