zoukankan      html  css  js  c++  java
  • 后台开发中的静态资源映射

    Tomcat配置

    修改server.xml配置的方式
    缺点显而易见,但凡知道地址,就可以下载文件,只要愿意,甚至把一整个服务器的文件载下来,因为独立于系统之外,系统内部代码很难限制文件的访问。

    <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
    
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log" suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
               
      <Context path="/rgyx_files" docBase="C:/xxxxx" reloadable="true" crossContext="true"></Context>
    </Host>

    Spring配置方式

    一般的用途:将文件放到WEB-INF下的时候,需要进行路径映射。其实,mvc:resources 也可以将路径映射到项目外的磁盘目录。

        <mvc:resources mapping="/${mapping_path}/**" location="file:${local_path}/">
    
        <!-- 系统主题 -->
        <mvc:resources location="/WEB-INF/static/theme/" mapping="/theme/**" />
        <mvc:resources location="/WEB-INF/static/js/" mapping="/js/**" />
        <!-- 通用JS -->
        <mvc:resources location="/WEB-INF/static/lib/" mapping="/lib/**" />
        <!-- 通用资源 -->
        <mvc:resources location="/WEB-INF/static/res/" mapping="/res/**" />
        <!-- LTE 风格管理页面 -->
        <mvc:resources location="/WEB-INF/static/pages/" mapping="/pages/**" />

    SpringBoot的配置方式

    等价于上述的Spring的配置,缺点就是,如果访问的是文件夹,你可能是希望它能返回文件夹的文件列表,对于共享文件夹,会出现文件无法读取的现象。

        /**
         * 代码样例
         * @param registry -
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/seaboot/file/**").addResourceLocations("file:xxxxxxx");
                logger.debug("开放文件上传资源目录:" + uploadConstant.getRoot());
        }

    源代码实现

    因为是自己编码实现的,因此,要实现什么样的逻辑,都是可以的。将Url映射到我们的服务器磁盘,然后通过response写入文件即可。

    cn.seaboot.common.core的相关代码,可以在 https://gitee.com/seaboot/common 页面下载。

    如果你非常熟悉下载文件的相关代码,那么就能轻易读懂下面的代码,基本思路,就是从Http的Url中截取文件信息,然后将文件下载下来。

    import cn.seaboot.common.core.CommonUtils;
    import cn.seaboot.common.core.FastJsonUtils;
    import cn.seaboot.common.file.MimeUtils;
    import cn.seaboot.common.file.ResponseWriter;
    import cn.seaboot.web.jackson.Result;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import javax.annotation.PostConstruct;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.*;
    
    /**
     * 资源映射
     *
     * @author Mr.css
     * @date 2020-09-30 20:01:38
     */
    @Controller
    @RequestMapping(value = "/sys/resource")
    public class ResourceController {
        /**
         * 资源映射
         */
        private Map<String, String> pathMapping = new HashMap<>();
    
        @PostConstruct
        public void init() {
            pathMapping.put("image", "C:/Users/postm/Desktop");
        }
    
        @RequestMapping(value = "**", method = RequestMethod.GET)
        public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
            if (!pathMapping.isEmpty()) {
                //start - Url路径截取,只是为了,从Url中获取文件的存储路径(按照自己的想法实现)
                String servletPath = request.getServletPath();
                String mappingPath = servletPath.substring(14);
                int idx = mappingPath.indexOf('/');
                if (idx < 0) {
                    idx = mappingPath.length();
                }
                String key= mappingPath.substring(0, idx);
                String path = pathMapping.get(key);
                String value = mappingPath.substring(idx);
                //end --
                File file = new File(path + value);
                if (file.exists()) {
                    if (file.isFile()) {
                        //如果是文件,则提供文件的预览功能
                        ResponseWriter.create(request, response)
                            .setPreviewName(file.getName())
                            .write(new FileInputStream(file));
                    } else {
                        //如果是文件夹,则列出文件夹下的全部文件,写成JSON数据
                        List<Map<String, Object>> list = genFileList(file);
                        Result result = Result.succeed(list);
                        String json = FastJsonUtils.toJSONString(result);
                        ResponseWriter.create(request, response)
                            .setContentType(MimeUtils.MIME_JSON)
                            .write(json);
                    }
                } else {
                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                }
            }
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    
        private List<Map<String, Object>> genFileList(File directory) {
            File[] files = directory.listFiles();
            if (CommonUtils.isEmpty(files)) {
                return Collections.emptyList();
            } else {
                List<Map<String, Object>> list = new ArrayList<>(files.length);
                for (File file : files) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", file.getName());
                    map.put("length", file.length());
                    map.put("lastModified", file.lastModified());
                    map.put("canRead", file.canRead());
                    map.put("canWrite", file.canWrite());
                    list.add(map);
                }
                return list;
            }
        }
    }

     效果如下:

    通过http请求,就可以获取电脑桌面的全部文件信息。

  • 相关阅读:
    vue Bus事件用法与bug
    移动端超出三行显示第三行显示省略号+查看全部
    Vuex
    vue点击旋转,再点击复原
    vue项目(cli-3)替换浏览器logo
    悬浮广告(二)vue版本
    悬浮广告(一)html版本
    vue下获得经纬度省市区
    一个可以输出当前移动设备机型(安卓,ios)系统版本的html页面
    谱聚类(spectral clustering)原理总结
  • 原文地址:https://www.cnblogs.com/chenss15060100790/p/13843211.html
Copyright © 2011-2022 走看看