【亚伟-EASCloud系列】之单据中添加图片上传功能的两种实现方式

栏目:eas cloud知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

【亚伟-EASCloud系列】之单据中添加图片上传功能的两种实现方式


最近在做一个功能,EAS的单据中支持在单据编辑界面上传图片,下面总结了两种实现方式,供大家参考。

 

方式一、数据库存储图片的方式

    该方式使用数据库字段存储图片数据;

    优点:开发实现方式简单,快捷;

    缺点:图片文件大小时对数据库有影响;

 

    实现方式如下:

        1、新增一个业务单元,在EditUI中添加KDLable控件,并设置控件的MouseClicked事件;

    image.webp

     image.webp



        2、修改实体,在实体中新增字段类型为byteArray的图片字段photo;修改table,添加字段类型为BLOB的数据库字段FPhoto;并将数据库字段与实体字段绑定。

    image.webp

    image.webp



        3、在XXEditUI.java文件中添加实现代码

    /**
     * 图片控件鼠标点击事件
     */
    @Override
    protected void labpic_mouseClicked(MouseEvent e) throws Exception {
       int count = e.getClickCount();
       if (count == 2) {//双击选择图片
           selectPhoto(); 
       }else if(count == 3){//3击删除图片,此处为示例,可采用其他方式删除
           deletePhoto();
       }
    }
   
    /**
     * 删除图片
     */
    private void deletePhoto() {
       int i = MsgBox.showConfirm2(this, "是否确认删除图片?");     
       if (MsgBox.OK == i) {       
           labpic.setIcon(null);         
           editData.setPhoto(null);   
       }
    }
 
    /**
     * 选择图片
     */
    private void selectPhoto() throws Exception{
       KDFileChooser fileChooser = new KDFileChooser();
       fileChooser.setFileFilter(new PictureFilter());
     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
       int result =fileChooser.showOpenDialog(this);
       if(result == 0) {
           java.io.File file = fileChooser.getSelectedFile();
           byte[] bt =HRUtil.convertFileTOBytes(file);
           Image image = ImageIO.read(new ByteArrayInputStream(bt));
          
           KDImageIcon icon = new KDImageIcon(image);
           labpic.setIcon(icon);
           editData.setPhoto(bt);
       }
    }
   
    /**
     * 打开单据时加载图片
     */
    @Override
    public void onShow() throws Exception {
       super.onShow();
       if(UIRuleUtil.isNotNull(editData.getPhoto())) {
           Image image =ImageIO.read(new ByteArrayInputStream(editData.getPhoto()));
           labpic.setIcon(new KDImageIcon(image));
       }
    }
 
    /**
     * 将photo字段添加到selector中
     */
    @Override
    public SelectorItemCollection getSelectors() {
       SelectorItemCollection sic = super.getSelectors();
       sic.add(new SelectorItemInfo("photo"));
       return sic;
    }

        4、实现效果如图


    image.webp


    方式二、附件存储图片的方式

        该方式是使用附件存储的方式存储图片数据;

        优点:采用附件方式存储,对数据库影响较小,可使用标准的附件管理功能进行查看管理;

        缺点:开发实现代码量相比数据库存储方式多,测试时需要多关注细节控制;

 

    实现方式如下:

        1、新增一个业务单元,在EditUI中添加KDLable控件,并设置控件的MouseClicked事件;

        image.webp

       image.webp



    2、在XXEditUI.java文件中添加实现代码

    BufferedImage Image;//图片文件
    private String pictureId = "";//图片附件ID
    private File pictureFile;//图片文件
 
    /**
     * 页面加载时展示图片
     */
    @Override
    public void loadFields() {
       super.loadFields();
 
       try {
           ClearImage();
           getPicture();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
 
    /**
     * 页面打开时加载图片
     */
    @Override
    public void onLoad() throws Exception {
       super.onLoad();
 
       getPicture();
    }
 
    /**
     * 图片控件点击事件
     */
    @Override
    protected void labpic_mouseClicked(MouseEvent e) throws Exception {
       if (e.getClickCount() == 2) {
           showAsOriginaSize();// 双击查看图片
       } else {
           if (e.getButton() != 3) {
              return;
           }
 
           KDPopupMenu popupMenu = CreatePopuMenu();// 添加右键菜单及右键菜单监听事件
           add(popupMenu);
           popupMenu.show(e.getComponent(), e.getX(), e.getY());
       }
    }
 
    /**
     * 添加右键菜单并添加监听事件
     */
    KDPopupMenu CreatePopuMenu() {
       //右键上传图片菜单及监听事件
       KDPopupMenu popuMenu = new KDPopupMenu();
       KDMenuItem mItemAdd = new KDMenuItem(EASResource.getString("com.kingdee.eas.basedata.master.material.MaterialResource","ADD_Image"));
 
       ActionListener lstAdd = new ActionListener() {
           public void actionPerformed(ActionEvent e) {
              try {
                  TestPicEditUI.this.showAddImageDialog();
              } catch (Exception e1) {
                  ExceptionHandler.handle(e1);
              }
           }
       };
       mItemAdd.addActionListener(lstAdd);
       popuMenu.add(mItemAdd);
 
      
       if (this.Image != null) {
           //右键另存图片菜单及监听事件
           KDMenuItem mItemSave = new KDMenuItem(EASResource.getString("com.kingdee.eas.basedata.master.material.MaterialResource","SAVE_Image"));
           ActionListener lstSave = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      TestPicEditUI.this.showSaveDialog();
                  } catch (Exception e1) {
                     ExceptionHandler.handle(e1);
                  }
              }
           };
           mItemSave.addActionListener(lstSave);
           popuMenu.add(mItemSave);
          
           //右键删除图片菜单及监听事件
           KDMenuItem mItemDelete = new KDMenuItem(EASResource.getString("com.kingdee.eas.basedata.master.material.MaterialResource","DELETE_Image"));
           ActionListener lstDelete = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      TestPicEditUI.this.showdeleteImage();
                  } catch (Exception e1) {
                     ExceptionHandler.handle(e1);
                  }
              }
           };
           mItemDelete.addActionListener(lstDelete);
           popuMenu.add(mItemDelete);
 
           //右键查看图片菜单及监听事件
           KDMenuItem mItemShowOriginaSize = new KDMenuItem(EASResource.getString("com.kingdee.eas.basedata.master.material.MaterialAddResource","ShowAsOriginaSize"));
           ActionListener lstShowOriginaSize = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      TestPicEditUI.this.showAsOriginaSize();
                  } catch (Exception e1) {
                     ExceptionHandler.handle(e1);
                  }
              }
           };
        mItemShowOriginaSize.addActionListener(lstShowOriginaSize);
           popuMenu.add(mItemShowOriginaSize);
 
           //单据查看状态时,清楚上传和删除菜单
           if (OprtState.VIEW.equals(getOprtState())) {
              mItemAdd.removeActionListener(lstAdd);
              popuMenu.remove(mItemAdd);
               mItemDelete.removeActionListener(lstDelete);
              popuMenu.remove(mItemDelete);
           }
       }
 
       return popuMenu;
    }
 
    /**
     *  上传图片
     */
    private void showAddImageDialog() throws Exception {
       //只有保存的单据才可以上传图片
       if ((((this.editData == null) || (this.editData.getId() == null) || (this.editData.getId().toString().equals(""))))&& (((this.editData == null) || (this.editData.getId() == null)))) {
        MsgBox.showError(EASResource.getString("com.kingdee.eas.basedata.master.material.MaterialResource","msgBox_noBaseInfo"));
 
           SysUtil.abort();
       }
 
       if (getOprtState().equals(OprtState.VIEW)) {
           return;
       }
       File file = chooseFileByDialog(this);
       if (file != null) {
           showImage(file);
           if (this.pictureFile != null) {
              String boid = this.editData.getId().toString();
              AttachmentClientManager acm = AttachmentManagerFactory.getClientManager();
 
              String[] attid = acm.getAttachmentIDsByBoIDAndCode(this.editData.getId().toString(), "picture");
              if (attid.length >= 1) {
                  acm.deleteOneAssociation(boid, attid[0]);
              }
              this.pictureId = acm.addNewAttachment(boid, this.pictureFile,"picture");
              this.pictureFile = null;
           }
       }
    }
 
    /**
     * 选择图片文件
     */
    private File chooseFileByDialog(Component owner) {
       File retFile = null;
       boolean isRetFileValid = false;
 
       KDFileChooser fc = new KDFileChooser(System.getProperty("user.home"));
       fc.setFileFilter(new PictureFilter());// 添加图片文件过滤
       fc.setFileSelectionMode(0);
       fc.setMultiSelectionEnabled(false);//设置不能多选
 
       while (!(isRetFileValid)) {
           int retVal = fc.showOpenDialog(owner);
           if (retVal == 1) {
              return retFile;
           }
           retFile = fc.getSelectedFile();
           if (retFile.exists()) {
              if (retFile.length() < 52428800L) {// 不能大于50M
                  isRetFileValid = true;
              }else{
               MsgBox.showInfo(Resrcs.getString("FileSizeNotAllowed"));
                  retFile = null;
              }
           }else{
               MsgBox.showInfo(Resrcs.getString("FileNotExisted"));
              retFile = null;
           }
       }
 
       return retFile;
    }
 
    /**
     * 另存图片
     */
    private void showSaveDialog() {
       KDFileChooser m_chooserSave = new KDFileChooser();
       m_chooserSave.setFileFilter(new PictureFilter());
 
       int result = m_chooserSave.showSaveDialog(this);
       if (result != 0) {
           return;
       }
       File f = m_chooserSave.getSelectedFile();
       saveComponentToJPEG(f.getAbsolutePath() + f.getName() + ".webp");
    }
 
    /**
     * 另存为jpg格式图片
     */
    private void saveComponentToJPEG(String fileName) {
       try {
           BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
 
           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
           JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam(this.Image);
 
           jep.setQuality(1.0F, false);
           encoder.setJPEGEncodeParam(jep);
           encoder.encode(this.Image);
           bos.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
 
    /**
     * 删除图片
     */
    private void showdeleteImage() throws Exception {
       if ((((this.editData == null) || (this.editData.getId() == null) || (this.editData.getId().toString().equals(""))))&& (((this.editData == null) || (this.editData.getId() == null)))) {
        MsgBox.showError(EASResource.getString("com.kingdee.eas.basedata.master.material.MaterialResource","msgBox_noBaseInfo"));
           SysUtil.abort();
       }
 
       if (getOprtState().equals(OprtState.VIEW)) {
           return;
       }
       if (confirmRemove()) {
           if ((this.pictureId != null) && (!(this.pictureId.equals("")))) {
              deleteImage();
           }
           ClearImage();
       }
    }
 
    /**
     *  查看图片
     */
    private void showAsOriginaSize() {
       if (this.Image == null)
           return;
       Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
       int width = this.Image.getWidth();
       int height = this.Image.getHeight();
       if (width + 15 > dimension.width) {
           width = dimension.width - 15;
       }
 
       if (height + 35 > dimension.height) {
           height = dimension.height - 55;
       }
 
       JLabel picLabel = new JLabel();
       ImageIcon imageIcon = new ImageIcon(this.Image);
       picLabel.setIcon(imageIcon);
       JScrollPane picScrollPane = new JScrollPane(picLabel, 20, 30);
 
       JFrame picFrame = new JFrame();
       picFrame.getContentPane().setLayout(new BorderLayout());
       picFrame.getContentPane().add(picScrollPane, "Center");
 
       picFrame.setSize(width + 15, height + 35);
       picFrame.show();
    }
 
    /**
     * 删除附件中存储的图片信息
     */
    private void deleteImage() throws Exception {
       String boid = this.editData.getId().toString();
       AttachmentClientManager acm = AttachmentManagerFactory.getClientManager();
       String[] attid = acm.getAttachmentIDsByBoIDAndCode(this.editData.getId().toString(), "picture");
       if (attid.length >= 1){
           acm.deleteOneAssociation(boid, attid[0]);
       }
    }
 
    /**
     * 清除界面图片信息
     */
    private void ClearImage() throws Exception {
       this.Image = null;
       this.pictureFile = null;
       this.pictureId = "";
       this.labpic.setIcon(null);
    }
 
    /**
     * 获取附件中的图片信息
     */
    private void getPicture() throws Exception {
       if (this.editData == null) {
           return;
       }
       if ((this.editData.getId() == null) || (this.editData.getId().toString().equals(""))) {
           return;
       }
      
       AttachmentClientManager acm = AttachmentManagerFactory.getClientManager();
       String[] attid = acm.getAttachmentIDsByBoIDAndCode(this.editData.getId().toString(), "picture");
       if (attid.length >= 1) {
           ComplexAttachmentInfo attinfo = acm.getComplexAttachmentInfo(attid[0]);
 
           if (attinfo != null) {
              byte[] data = attinfo.getContent();
              showImage(data);
              this.pictureId = attid[0];
           } else {
              this.pictureId = "";
           }
       } else {
           this.pictureId = "";
       }
    }
 
    /**
     * 展示图片file
     */
    private void showImage(File file) throws Exception {
       BufferedImage img = ImageIO.read(file);
       if (img == null) {
           throw new MaterialBaseException(MaterialBaseException.INVALID_IMAGE);
       }
       KDImageIcon icon = new KDImageIcon(img);
       this.labpic.setIcon(icon);
       this.Image = img;
       this.pictureFile = file;
    }
 
    /**
     * 展示图片byte
     */
    private void showImage(byte[] data) throws Exception {
       ByteArrayInputStream bais = new ByteArrayInputStream(data);
       BufferedImage img = ImageIO.read(bais);
       KDImageIcon icon = new KDImageIcon(img);
       this.labpic.setIcon(icon);
       this.Image = img;
       this.pictureFile = null;
       bais.close();
    }


    3、实现效果

     image.webpimage.webp

     image.webp


喜欢的来一波点赞、收藏、转发【emoji】,欢迎各位小伙伴补充知识点【emoji】        


欢迎各位小伙伴补充其他实现方式,共同交流!

EAS单据中添加图片上传功能的两种实现方式.docx

【亚伟-EASCloud系列】之单据中添加图片上传功能的两种实现方式

最近在做一个功能,EAS的单据中支持在单据编辑界面上传图片,下面总结了两种实现方式,供大家参考。 方式一、数据库存储图片的方式 该...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息