NutzCN Logo
精华 nutzboot上传文件路径问题
发布于 2081天前 作者 文涛(wentao) 2460 次浏览 复制 上一个帖子 下一个帖子

方法加了适配器

@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/tmp" })

上传后地址:

/Volumes/Data/git/apk_manage/./webapp/tmp/00/00/00/00/00/00/00/01.log

请问还有其他方式控制文件路径和文件名吗

11 回复

用相对路径吧,nutzboot发布时是个jar

我现在用了相对路径,但是为什么会生成这么多层文件夹?

` /Volumes/Data/git/apk_manage/upload/00/00/00/00/00/00/00/01.log

你需要把临时文件copy到存储文件夹

那请问如何获取客户端原始文件名?
我用了

getName();
getAbsolutePath();
getCanonicalPath();
f.getPath();

用TempFile对象接收可以了

@wendal
刚刚试了相对路径

@AdaptBy(type = UploadAdaptor.class, args = { "tmp" })

打jar后上传文件,结果在tmp目录是在jar包的上一层创建的;
然后看到 https://gitee.com/nutz/nutzboot/blob/dev/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java 源码
里面的getBasePath方法始终返回.

    // 获取应用程序绝对路径
    protected String getBasePath() {
        try {
            String basePath = appContext.getMainClass().getProtectionDomain().getCodeSource().getLocation().getPath();
            int lastIndex = basePath.lastIndexOf('/');
            if (lastIndex < 0) {
                lastIndex = basePath.lastIndexOf('\\');
            }
            basePath = basePath.substring(0, lastIndex);
            basePath = URLDecoder.decode(basePath, Encoding.UTF8);
        } catch (Throwable e) {
        }
        return ".";
    }

是不是这里的问题,这个方法当时是我封装的,但我是返回的basePath

知道了,是java命令在哪个目录执行,就在哪里为相对路径创建tmp目录

请问下args里的参数可以是动态的吗,我想自己算出当前目录,不用那个相对路径

自定义适配器类

继承了一下UploadAdaptor,重写了一下方法就搞定了分享下:

    public CustomUploadAdaptor(String path) {
        super(MainLauncher.getPath(path));
    }

路径获取方法

    // 获取应用程序绝对路径
    public static String getBasePath() {
        try {
            String basePath = MainLauncher.class.getProtectionDomain().getCodeSource().getLocation().getPath();
            int lastIndex = basePath.lastIndexOf('/');
            if (lastIndex < 0) {
                lastIndex = basePath.lastIndexOf('\\');
            }
            basePath = basePath.substring(0, lastIndex);
            basePath = URLDecoder.decode(basePath, Encoding.UTF8);
            return basePath;
        } catch (Throwable e) {
            return ".";
        }

    }

    // 根据目录和文件名拼接绝对路径
    public static String getPath(String... names) {
        String path = getBasePath();
        String tmp = Strings.join(File.separator, names);
        if (new File(tmp).exists() && new File(tmp).getAbsolutePath().equals(tmp)) {
            return tmp;
        }
        else
            return path + File.separator + tmp;
    }

很好很强大

添加回复
请先登陆
回到顶部