zoukankan      html  css  js  c++  java
  • ZK 上傳圖片和顯示圖片(保存在文件夹)

    上傳圖片:

    前台代码:

    <div  width="19%" style="float:right;clear:right;overflow:hidden;">
        <grid>
        <columns>
         <column width="100%"></column>
        </columns>
        <rows>
         <row><div width="100px" height="200px">
           <image id="memberPhoto" src=""/>
         </div></row>
        </rows>
        </grid>
        <separator />
        <button label="上傳相片" onClick="winInfo.upload()"/>
       </div>

    后台代码:

    upload Event:

    public void upload() throws InterruptedException,IOException,SQLException{
      Configuration config=this.getDesktop().getWebApp().getConfiguration();
      config.setMaxUploadSize(1024); // 單位 KB ,如果為負則不限制大小
      Object media=org.zkoss.zul.Fileupload.get("選擇文件","相片上傳");
      if(media==null)
       return;
      if(!(media instanceof org.zkoss.image.Image)){
       Messagebox.show("此文件不是圖片類型,請檢查!","Error",Messagebox.OK,Messagebox.ERROR);
       return;
      }
      org.zkoss.image.Image image=(org.zkoss.image.Image)media;
      String fileName=image.getName().toLowerCase();
      memberPhotoType=fileName.substring(fileName.length()-3);//图片的类型
      memberPhotoInputStream=image.getStreamData();//图片的InputStream
      Image memberImage=(Image)this.getFellow("memberPhoto");
      memberImage.setContent((org.zkoss.image.Image)media);//将图片放入<image>里
     }

    public void save() throws IOException{

       Desktop dtp=Executions.getCurrent().getDesktop();

       String realPath=dtp.getSession().getWebApp().getRealPath("/upload/member_photo/")+"/";查找存放文件的路径   

       File file=new File(realPath+"文件名");

       Files.copy(file,memberPhotoInputStream);

       Files.close(r);

    }

    显示图片

    public void load() throws IOException{

      Desktop dtp=Executions.getCurrent().getDesktop();

      String realPath=dtp.getSession().getWebApp().getRealPath("/upload/member_photo/")+"/"+memberPhotoPath;

      AImage aImg=new AImage(realPath);//这个是关键

      Image memberImage=(Image)this.getFellow("memberPhoto");

      memberImage.setContent(aImg);

    }

  • 相关阅读:
    spring 动态创建数据源
    现有‘abcdefghijkl’12个字符,将其所有的排列按字典序进行排序,给出任意一组排列,说出这租排列在所有排列中是第几小的
    javaweb项目运行时错误
    StringUtils.isEmpty和StringUtils.isBlank用法和区别
    启动项目时tomcat问题汇总
    hibernate 在web.xml中配置的作用
    Java几种常见的编码方式
    struts学习总结
    Javaweb开发中关于不同地方出现的绝对路径和相对路径
    解决中文乱码问题
  • 原文地址:https://www.cnblogs.com/TankMa/p/1962135.html
Copyright © 2011-2022 走看看