js:

<script type="text/javascript">
        $(function(){
            //使用uploadify组件后,每次发送请求都会新建一个session
            $('#upload_file').uploadify({
                "uploader":'js/uploadify.swf', //上传使用的flash文件地址
                "multi":'true', //是否允许上传多个文件
                "fileExt":'*.*', //设置允许上传的文件后缀
                "cancelImg":'images/cancel.png', //取消图片的路径
                "auto":false, //是否自动上传
                "script":"bookAction!doAdd.action", //上传处理页面
                "fileDataName":"fileUpload", //和Action中的FormFile指定的名称对应
                "onError" : function (event,ID,fileObj,errorObj) {
                       alert(errorObj.type + ' Error: ' + errorObj.info);
                     },
                "onAllComplete":function(){
                        location.href = 'bookAction!showVideo.action';
                    }
            });
        });

        function toUpload(){
            document.getElementById("toUpload").disabled = true;
            $('#upload_file').uploadifyUpload();
        }
    </script>

 

引入:

<link rel="stylesheet" type="text/css" href="css/uploadify.css">
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/jquery.uploadify.v2.1.4.min.js"></script>

页面:

 

<input id="upload_file" type="file"/> <br/>
    <input type="button" id="toUpload" value="开始上传" onclick="toUpload()">

 

action:

public String doAdd() throws Exception {
        InputStream is = new FileInputStream(fileUpload);
        String filePath = ServletActionContext.getServletContext().getRealPath("/file");
        int pos = fileUploadFileName.lastIndexOf(".");
        Date today = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String day = df.format(today);
        String newfileUploadFileName = day + fileUploadFileName.substring(pos) + ".zip";
        String fileName = filePath + File.separator + newfileUploadFileName;
        File file = new File(fileName);
        OutputStream os = new FileOutputStream(file);
        int length = 0;
        byte[] b = new byte[1024*1024];
        while ((length = is.read(b)) != -1) {
            os.write(b, 0, length);
        }
        os.close();
        is.close();
        TblVideo tblVideo = new TblVideo();
        tblVideo.setVideoName(fileUploadFileName);
        tblVideo.setVideoPath("file/" + newfileUploadFileName);
        videoDao.addVideo(tblVideo);
        ServletActionContext.getResponse().getWriter().write("{fileName:'"+tblVideo.getVideoName()+"'}");
        return null;
    }