`

zk fileupload

    博客分类:
  • Ajax
阅读更多
可以限制文件大小,类型,支持中文,多文件上传,并且可以存到指定目录
直接上代码,有问题直接提

<?xml version="1.0" encoding="GBK"?>
<window>
    <label value="test"></label>
    <button label="upload">
         <attribute name="onClick">
             <![CDATA[
             import org.zkoss.zk.ui.util.Configuration;
             import java.io.*;
             import org.zkoss.io.Files;
 
             Desktop desktop = self.getDesktop();
             Configuration conf = desktop.getWebApp().getConfiguration();
             conf.setMaxUploadSize(1024);
            //解决中文问题,如果上传的文件中有中文
            conf.setUploadCharset("gbk");
            //如果文本文件的话,返回的实际是AMedia类型
            Object media = Fileupload.get();

            //多文件

            //Object[] mediaArray = Fileupload.get(5);
            if(media!=null){
                 String fileName =  media.getName();
                 if(!fileName.endsWith(".txt")){
                       alert("not a txt file");

                       return;
                 }
   
           /**
            * 用Reader的方式,实际是StringReader
             */
   
              Reader r = media.getReaderData();
              File f = new File("c:\\temp\\"+fileName);
   
               /*
               //可以获得相对路径
                 File f = new File(Executions.getCurrent().getDesktop().getWebApp().getRealPath("medias") + "/"+ media.getName());
               */
   
                 if(!f.exists()){
                        f.createNewFile();
                 }
               

                //Files类是由ZK提供的
                Files.copy(f,r,null);
                Files.close(r);
   
                /*
                  //用InputStream的实现方式
                  InputStream ins = new ByteArrayInputStream(media.getStringData().getBytes());
                  File f = new File("c:\\temp\\"+fileName);
                  if(!f.exists()){
                         f.createNewFile();
                   }
                  OutputStream out = new FileOutputStream(f);
                   byte[] buf=new byte[1024];
                    int len;
                   while((len=ins.read(buf))>0){
                   out.write(buf,0,len);
            }
            out.close();
            ins.close();
    
              */
               alert("file created");

           }
          //alert(media.getStringData().length());
          //alert(media.getName());
  
      ]]>
      </attribute>
   </button>
   <vbox id="file">
    </vbox>

</window>

 

<window title="Fileupload Demo" border="normal">
    <image id="image"/>
    <button label="Upload">
        <attribute name="onClick">{
            Object media = Fileupload.get();
            if (media instanceof org.zkoss.image.Image)
                image.setContent(media);
            else if (media != null)
                Messagebox.show("Not an image: "+media, "Error",
                Messagebox.OK, Messagebox.ERROR);
            }
        </attribute>
    </button>
</window>
 
<window title="fileupload demo" border="normal">
    <button label="Upload">
        <attribute name="onClick"><![CDATA[{
            Object media = Fileupload.get(5);
            if (media != null)
            for (int j = 0; j < media.length; ++j) {
                if (media[j] instanceof org.zkoss.image.Image) {
                    Image image = new Image();
                    image.setContent(media[j]);
                    image.setParent(pics);
                } else if (media[j] != null) {
                    Messagebox.show("Not an image: "+media[j], "Error",
                    Messagebox.OK, Messagebox.ERROR);
                }
            }
        }]]>
        </attribute>
    </button>
    <vbox id="pics"/>
</window>
 

 

分享到:
评论
1 楼 xiao_feng68 2011-03-04  
有个疑问想问博主:问什么直接使用组件事件得到的media是都是按字节流读取的文件。
而通过get()出来的media要如此呢?

相关推荐

    开发工具 commons-fileupload-1.3.2

    开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2...

    commons-fileupload-1.2.2

    commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-...

    commons-fileupload-1.4-API文档-中文版.zip

    赠送jar包:commons-fileupload-1.4.jar; 赠送原API文档:commons-fileupload-1.4-javadoc.jar; 赠送源代码:commons-fileupload-1.4-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.4.pom; 包含...

    commons-fileupload-1.3.3-API文档-中文版.zip

    赠送jar包:commons-fileupload-1.3.3.jar; 赠送原API文档:commons-fileupload-1.3.3-javadoc.jar; 赠送源代码:commons-fileupload-1.3.3-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.3.3.pom;...

    ASP.NET.FileUpload控件

    FileUpload控件的基本用法 .

    commons commons-fileupload历史版本jar包集合

    commons commons-fileupload历史版本jar包集合,包括src源码 附件列表: commons-fileupload-1.0.zip commons-fileupload-1.1.1.zip commons-fileupload-1.1.zip commons-fileupload-1.2.1-bin.zip commons-...

    Apache Commons FileUpload 1.3.2 released

    Commons FileUpload 1.3.2. The Apache Commons FileUpload component provides a simple yet flexible means of adding support for multipart file upload functionality to servlets and web applications. ...

    fileupload

    &lt;jsp:useBean id="myUpload" scope="page" class="fileUpload.upBean" /&gt; //初始化工作 myUpload.initialize(pageContext); //设定允许的文件后缀名 //myUpload.setAllowedExtList("gif,jpg"); //设定允许...

    commons-fileupload commons-fileupload-1.2.1-bin

    commons-fileupload-1.2.1-bin commons-fileupload 文件上传 commons-fileupload-1.2.1-bin commons-fileupload 文件上传 jar包 及相关文档

    FileUpload笔记

    FileUpload笔记FileUpload笔记FileUpload笔记FileUpload笔记FileUpload笔记FileUpload笔记

    Commons FileUpload 1.4 + IO 2.2

    Java借助commons-fileupload组件实现文件上传所需包 Apache Commons FileUpload 1.4 (requires Java 1.6 or later) Note: This version of Commons FileUpload also requires Commons IO 2.2.

    FileUpload美化CSS

    FileUpload 自带样式实在难看。 试了很多种方法,最终用了简单的css,简单美化了下。 其它思路:通过图片的click事件,触发FileUpload的click事件。但存在兼容性问题。并不能完全兼容所以浏览器。

    FileUpload

    文件上传 文件下载 FileUpload

    commons-fileupload-1.3.3

    commons-fileupload-1.2.1.jar包;commons-lang-2.3.jar,在使用组件实现文件上传时候要注意前后台的一致性,注意在后台定义File文件对象名要与jsp页面中的file标签name名字一致,前台form标签属性一定要设置为...

    commons-fileupload及源码

    commons-fileupload apache开源项目,实现文件上传下载功能 ssh上传时必配

    使用FileUpload组件实现上传功能

    使用FileUpload组件实现上传功能,供大家一起共同分享学习。

    FileUpload组件使用方法

    FileUpload组件使用方法, FileUpload组件使用方法, FileUpload组件使用方法,

    commons-fileupload-1.4.jar

    commons-fileupload-1.4.jar

    commons-fileupload-1.3.1.jar

    commons-fileupload-1.3.1.jar

    fileUpload组件所需jar包

    jsp向服务器进行文件上传所需的fileupload组件所需的jar包

Global site tag (gtag.js) - Google Analytics