通过七牛云存储实现对本地资源的存储操作

  def upload(request,params){
        //文件保存目录路径
        String savePath = request.getRealPath("/") + "attached/";
        //文件保存目录URL
        //String saveUrl  = request.getContextPath() + "/attached/";     
        //定义允许上传的文件扩展名
        HashMap extMap = new HashMap();
        extMap.put("image", "gif,jpg,jpeg,png,bmp");
        extMap.put("flash", "swf,flv");
        extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
        extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
        //最大文件大小
        long maxSize = 1000000;
        //检查目录
        File uploadDir = new File(savePath);
        if(!uploadDir.isDirectory()){
//            render(getError("上传目录不存在。"));
        }
        //检查目录写权限
        if(!uploadDir.canWrite()){
//            render(getError("上传目录没有写权限。"));
        }
        String dirName = request.getParameter("dir");
        if (dirName == null) {
            dirName = "image";
        }

        if(!extMap.containsKey(dirName)){
        }

        //创建文件夹
        savePath += dirName + "/";
        //saveUrl += dirName + "/";
        File saveDirFile = new File(savePath);
        if (!saveDirFile.exists()) {
            saveDirFile.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        savePath += ymd + "/";
        //saveUrl += ymd + "/";
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        try {
            def f = request.getFile('image')
            if(!f)
                f = request.getFile('imgFile')
            if(!f.empty){
                def fileName=f.getOriginalFilename() //得到文件名称
               // string localpath=f.getAbsolutePath();
               // println localpath
                String fileType = ".jpg";
                if(fileName!=null && fileName!=''){
                    fileType = fileName.substring(fileName.indexOf(".")) //得到文件类型
                }
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + fileType;
                def file= new File(savePath, newFileName);
                if(!file.exists()){
                    file.mkdirs()//如果file不存在自动创建
                }
                f.transferTo(file) //上传            
                def key;
                if(!f.isEmpty()){
                    String suffix = f.getOriginalFilename().substring(f.getOriginalFilename().lastIndexOf("."));
                    key =  UUID.randomUUID().toString() + suffix;
                  }
                
                Config.ACCESS_KEY = "BVV63KrFiv1O2tMn4_Og5IW7gWVEWcw2XkJFfUGJ";
                Config.SECRET_KEY = "jvkEGVGfoUegBmmH64slRcNEjVVXtGIDpEvV91pL";
                
                Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
                // 请确保该bucket已经存在
                String bucketName = "domain";
                PutPolicy putPolicy = new PutPolicy(bucketName);
                String uptoken = putPolicy.token(mac);
                PutExtra extra = new PutExtra();
                def localFile= savePath+newFileName;            
                PutRet ret = IoApi.putFile(uptoken, key, localFile, extra);
                if (ret.ok()) {
                    File tempfile=new File(localFile);
                    tempfile.delete();
                    //切换不同七牛账户注意更新url头部,以新的七牛账户httpURL为准
                    String url= "http://7xn3uy.com1.z0.glb.clouddn.com/" + "/" + key;
                    return url;
                } else {
                  return;
                }            
            }
        } catch (Exception e) {
            e.printStackTrace()
            return("")
        }
    }
    

转载于:https://www.cnblogs.com/yu0312chao/p/4881700.html

你可能感兴趣的:(通过七牛云存储实现对本地资源的存储操作)