NutzCN Logo
问答 nutz 有没有文件读取配置
发布于 1822天前 作者 Hamming 2437 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz 有没有文件读取配置
springboot 配置 拦截请求 /profile/** 直接读取文件
这个 nutz 有类似的支持吗

package com.ruoyi.framework.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用配置
 * 
 * @author ruoyi
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    /**
     * 首页地址
     */
    @Value("${shiro.user.indexUrl}")
    private String indexUrl;

    /**
     * 默认首页的设置,当输入域名是可以自动跳转到默认指定的网页
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry)
    {
        registry.addViewController("/").setViewName("forward:" + indexUrl);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 文件上传路径 */
        registry.addResourceHandler("/profile/**").addResourceLocations("file:" + RuoYiConfig.getProfile());

        /** swagger配置 */
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
10 回复

木有,写个入口方法(支持**)或者Filter呗

访问 路径 404
http://localhost:8091/open/file/get/mnt/upload/112321/201905/sqgmsnubhog4ip7crrqr3lped6.jpg

@IocBean
@At("/open/file")
public class UploadController {
    private static final Log log = Logs.get();

    @At("/get/**")
    @Ok("raw")
    public void get(String path,HttpServletRequest req, HttpServletResponse resp) {
        File file = new File(path);
        // 取得文件名。
        String filename = file.getName();
        // 取得文件的后缀名。
        String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

        // 以流的形式下载文件。
        try (InputStream fis  = new BufferedInputStream(new FileInputStream(path))){
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            resp.reset();
            // 设置response的Header
            resp.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            resp.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(resp.getOutputStream());
            resp.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

访问地址 还是404
http://localhost:8091/profile/mnt/upload/112321/201905/sqgmsnubhog4ip7crrqr3lped6.jpg

package io.nutz.nutzsite.common.starter;

import org.nutz.boot.starter.WebFilterFace;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.Mvcs;

import javax.servlet.*;
import java.io.*;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;


/**
 * 文件读取过滤器
 * @Author: Haimming
 * @Date: 2019-05-08 14:31
 * @Version 1.0
 */
@IocBean
public class FileFilterStarter implements WebFilterFace, Filter {

    @Override
    public String getName() {
        return "fileFilterStarter";
    }
    @Override
    public String getPathSpec() {
        return "/profile/**";
    }
    /**
     * 需要支持哪些请求方式
     *
     * @return 请求方式列表
     */
    @Override
    public EnumSet<DispatcherType> getDispatches() {
        return EnumSet.of( DispatcherType.FORWARD,
                DispatcherType.INCLUDE,
                DispatcherType.REQUEST,
                DispatcherType.ASYNC,
                DispatcherType.ERROR);
    }

    @Override
    public Filter getFilter() {
        return this;
    }

    @Override
    public Map<String, String> getInitParameters() {
        return new HashMap<String, String>();
    }

    @Override
    public int getOrder() {
        return 70;
    }

    @Override
    public void setServletContext(ServletContext sc) {

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        String url = Mvcs.getReq().getRequestURI();
        // path是指欲下载的文件的路径。
        String path =url.substring(url.indexOf("/profile"));
        File file = new File(path);
        // 取得文件名。
        String filename = file.getName();
        // 取得文件的后缀名。
        String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

        // 以流的形式下载文件。
        try (InputStream fis  = new BufferedInputStream(new FileInputStream(path))){
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            Mvcs.getResp().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            Mvcs.getResp().addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {

    }
}

优先级调下,然后getPathSpec return "/*"; 试试

url有.JPG就无法访问,有点就无法访问

有没有进doFilter方法?

过滤器 获取不到 URL

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        String url = Mvcs.getReq().getRequestURI();
        // path是指欲下载的文件的路径。
        String path =url.substring(url.indexOf("/profile"));
        File file = new File(path);
        // 取得文件名。
        String filename = file.getName();
        // 取得文件的后缀名。
        String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

        // 以流的形式下载文件。
        try (InputStream fis  = new BufferedInputStream(new FileInputStream(path))){
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            Mvcs.getResp().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            Mvcs.getResp().addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
添加回复
请先登陆
回到顶部