文章标签 ‘swfupload’
20141 月2

Nutz:使用第三方JS控件实现多文件上传的方法

适用于 Nutz+SWFUpload、Nutz+plupload 、Ueditor 等控件文件上传,验证用户身份。

   /**
     * 验证用户帐号,保存文件
     *
     * @param tmpFile
     * @param filetype
     * @param file_password
     * @param file_username
     * @param errCtx
     * @return
     */
    @At
    @Ok("raw")
    @AdaptBy(type = UploadAdaptor.class, args = "ioc:upload")
    public JSONObject uploadOneSave(@Param("Filedata") TempFile tmpFile, @Param("ueditor") String ueditor1, @Param("filetype") String filetype, @Param("title") String title, @Param("file_password") String file_password, @Param("file_username") String file_username, AdaptorErrorContext errCtx) {
        boolean ueditor = false;//是否是百度编辑器,编辑器对应的JS要做相应的修改
        if ("true".equals(StringUtil.null2String(ueditor1)))
            ueditor = true;
        System.out.println("ueditor::::::::::"+ueditor);
        JSONObject js = new JSONObject();
        if (errCtx != null) {
            if (errCtx.getAdaptorErr() != null) {
                if (ueditor) {
                    js.put("state", errorMsg(errCtx.getAdaptorErr()));
                } else {
                    js.put("error", errorMsg(errCtx.getAdaptorErr()));
                    js.put("msg", "");
                }
                System.out.println("js1::::::::::"+js.toString());
                return js;
            }
            for (Throwable e : errCtx.getErrors()) {
                if (e != null) {
                    if (ueditor) {
                        js.put("state", errorMsg(e));
                    } else {
                        js.put("error", errorMsg(e));
                        js.put("msg", "");
                    }
                    return js;
                }
            }
        }
        if ("".equals(StringUtil.null2String(file_username)) || "".equals(StringUtil.null2String(file_password))) {
            if (ueditor) {
                js.put("state", "错误:请配置文件服务器用户名及密码!");
            } else {
                js.put("error", "错误:请配置文件服务器用户名及密码!");
                js.put("msg", "");
            }
            return js;
        }
        Ioc ioc = new NutIoc(new JsonLoader("config/fileserver.json"));
        String u = ioc.get(FileServer.class, "fileserver").getUsername();
        String p = ioc.get(FileServer.class, "fileserver").getPassword();
        if (!u.equals(file_username) || !p.equals(DecodeUtil.Decrypt(file_password))) {
            if (ueditor) {
                js.put("state", "错误:文件服务器用户名或密码不正确!");
            } else {
                js.put("error", "错误:文件服务器用户名或密码不正确!");
                js.put("msg", "");
            }
            return js;
        }
        if (tmpFile == null || tmpFile.getFile().length() < 10) {
            if (ueditor) {
                js.put("state", "错误:文件大小不可小于10B!");
            } else {
                js.put("error", "错误:文件大小不可小于10B!");
                js.put("msg", "");
            }
            return js;

        }
        String filename = tmpFile.getMeta().getFileLocalName();
        File file = tmpFile.getFile();
        String suffixname = Files.getSuffixName(file).toLowerCase();
        String ss = FileType.getSuffixname(upload, filetype);
        if (!ss.contains(suffixname)) {
            if (ueditor) {
                js.put("state", "错误:不允许的文件扩展名,允许:" + ss);
            } else {
                js.put("error", "错误:不允许的文件扩展名,允许:" + ss);
                js.put("msg", "");
            }
            return js;
        }

        long len = tmpFile.getFile().length();

        filename = filename.substring(0, filename.lastIndexOf(".")) + "." + suffixname;
        String date = DateUtil.getToday();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fname = uuid + "." + Files.getSuffixName(file).toLowerCase();
        String dest = webPath(date, fname, suffixname);
        try {
            Files.move(file, new File(dest));
        } catch (IOException e) {
            e.printStackTrace();
            if (ueditor) {
                js.put("state", "错误:文件服务器IO异常!");
            } else {
                js.put("error", "错误:文件服务器IO异常!");
                js.put("msg", "");
            }
            return js;
        }
        JSONObject fs = new JSONObject();
        if (ueditor) {
            js.put("state", "SUCCESS");
            js.put("original", filename);
            js.put("url", "/upload/" + FileType.getFileType(upload, suffixname) + "/" + date + "/" + fname);
            js.put("title", title);
        } else {
            fs.put("filename", filename);
            fs.put("filepath", "/upload/" + FileType.getFileType(upload, suffixname) + "/" + date + "/" + fname);
            fs.put("filesize", StringUtil.getFileSize(len, 2));
            js.put("error", "");
            js.put("msg", fs);
        }
        return js;

    }

    /**
     * 获取上传路径,根据文件类型+日期生成路径
     *
     * @param date
     * @param fname
     * @param suffixname
     * @return
     */
    public String webPath(String date, String fname, String suffixname) {
        String newfilepath = Mvcs.getServletContext().getRealPath(
                "/upload/" + FileType.getFileType(upload, suffixname) + "/" + date
                        + "/");
        Files.createDirIfNoExists(newfilepath);
        return newfilepath + "\\" + fname;
    }

    /**
     * 根据异常提示错误信息
     *
     * @param t
     * @return
     */
    private String errorMsg(Throwable t) {
        if (t == null || t.getClass() == null) {
            return "错误:未知system错误!";
        } else {
            String className = t.getClass().getSimpleName();
            if (className.equals("UploadUnsupportedFileNameException")) {
                String name = upload.getContext().getNameFilter();
                return "错误:无效的文件扩展名,支持的扩展名:" + name.substring(name.indexOf("(") + 1, name.lastIndexOf(")")).replace("|", ",");
            } else if (className.equals("UploadUnsupportedFileTypeException")) {
                return "错误:不支持的文件类型!";
            } else if (className.equals("UploadOutOfSizeException")) {
                return "错误:文件超出" + StringUtil.getFileSize(upload.getContext().getMaxFileSize(), 2) + "MB";
            } else if (className.equals("UploadStopException")) {
                return "错误:上传中断!";
            } else {
                return "错误:未知错误!";
            }
        }
    }

项目部署根目录增加:crossdomain.xml 文件,解决上传控件跨域上传的问题(应用和文件服务器分开部署):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cross-domain-policy SYSTEM  
    "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd" >  
<cross-domain-policy>  
    <site-control permitted-cross-domain-policies="all" />  
    <allow-access-from domain="*" />  
    <allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
20141 月2

swfupload:动态传递参数的方法

SWFUpload.prototype.setPostParams = function (paramsObject) {
this.settings.post_params = paramsObject;
this.callFlash(“SetPostParams”, [paramsObject]);
};

 

swfu.setPostParams({ path:$(“#path”).val() });