Uploadify文件上传空间

1、JSP页面:

<input type="file" name="file_upload" id="file_upload1"/>

2、JS :

    // 初始化上传附件按钮
    init_upload();

    /**
     * 营业执照图片上传
     */

   function init_upload(){
    $("#file_upload1").uploadify({
        'swf': basePath + 'resources/uploadify/uploadify.swf',
        'uploader': basePath + 'basSupplierInfo/uploadFiles.do',
        'cancelImg': basePath + 'resources/uploadify/uploadify-cancel.png',
        //'langFile':basePath + 'resources/uploadify/lang.js',
        'preventCaching': true,    // 加随机数到URL后,防止缓存
        // 'folder': 'upload',
        'buttonText' : '选择文件',
         'progressData ': 'percentage',//显示上传进度方式
        //'queueID' : 'custom-queue',
        //'queueSizeLimit' : 99,    // 队列长度
        // 'method' : "post",
        'fileTypeExts' : '*.jpg;*.png;',  
        'removeCompleted' : false,// 上传完成后是否删除队列中的对应元素
        //'removeTimeout' : 10,//上传完成后多少秒后删除队列中的进度条,
        //'requeueErrors'    : true,    // 上传失败后重新加入队列
        // 'buttonCursor': 'hand',    // 上传按钮Hover时的鼠标形状
        'auto':true,   //是否自动上传
        'multi':false,  //多文件上传
        'uploadLimit' : 1, //最多上传文件数量
        'fileObjName':'file_upload',
        //'fileSizeLimit':2000,// 文件大小限制, 200 KB
        'onUploadSuccess' : function(file, data, response) {   //上传成功操作后
           $("#licensePicName").val(file.name);
           $("#licensePicUrl").val($.parseJSON(data).fileNameOfUUID);  //String转成json串
           var cancel=$("#"+file.id + " .cancel a");
               if (cancel) {
                   cancel.on('click',function () {
                       //通过uploadify的settings方式重置上传限制数量
                       $('#file_upload1').uploadify('settings','uploadLimit', ++uploadLimit1);
                       //防止手快多点几次x,把x隐藏掉
                       $(this).hide();
                   });
               }
        },
        'onUploadError' : function(file, errorCode, errorMsg, errorString){
            //alert(errorMsg);
        }      
    });

}

3、后台方法:

 /**
        * 处理文件上传
        * @param response
        * @param request
        * @return
        * @throws IOException
        */
        @RequestMapping(value="/uploadFiles",method=RequestMethod.POST)  
        @ResponseBody
        public Map<String, Object> upload(HttpServletResponse response,HttpServletRequest request) throws IOException{  
       
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String responseStr="";
        MultipartHttpServletRequest multipartRequest =(MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();    
        //String savePath = this.getServletConfig().getServletContext().getRealPath("");
                //savePath = savePath + "/uploads/";
        // 文件保存路径  ctxPath本地路径

/*String ctxPath=request.getSession().getServletContext().getRealPath("/")+File.separator+"uploadFiles";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");  
        String ymd = sdf.format(new Date());  
        ctxPath += File.separator + ymd + File.separator;  
        System.out.println("ctxpath="+ctxPath);
        // 创建文件夹  
        File file = new File(ctxPath);    
        if (!file.exists()) {    
            file.mkdirs();    
        }
        */
        //指定文件上传目录
        String ctxPath = PropertiesUtil.readValue("params.properties", "supplierRegisterFile.path");
        System.out.println("ctxpath="+ctxPath);
         
        String fileName = null;
        String fileNameOfUUID = null;
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {    
        // 上传文件   
        MultipartFile mf = entity.getValue();  
        fileName = mf.getOriginalFilename();//获取原文件名  
        System.out.println("filename="+fileName);
        String uuid = UUID.randomUUID().toString().toUpperCase();
        fileNameOfUUID = uuid+fileName.substring(fileName.lastIndexOf("."));

//fileNameOfUUID=fileName.substring(0,fileName.lastIndexOf("."))+uuid+fileName.substring(fileName.lastIndexOf("."));
        System.out.println("fileNameOfUUID="+fileNameOfUUID);
        File uploadFile = new File(ctxPath + File.separator + fileNameOfUUID);
        try {  
        FileCopyUtils.copy(mf.getBytes(), uploadFile);  
           responseStr="上传成功";  
           } catch (IOException e) {  
            responseStr="上传失败";  
               e.printStackTrace();  
               }
        }   
        resultMap.put("fileName", fileName);
        resultMap.put("fileNameOfUUID", fileNameOfUUID);
        return resultMap;    
        }

 

转载于:https://my.oschina.net/u/2615549/blog/903325

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值