kkfileview(6)

2021SC@SDUSC

application.properties配置文件的作用

application.properties是SpringBoot框架中的一个全局的配置文件,是核心配置文件。这个配置文件主要用来配置数据库的连接,日志相关配置,以及以下功能:

1)自定义属性与加载: [在配置文件中自定义属性后,可以通过@Value("${属性 名}")注解加载对应的配置属性]

2)参数间的引用: 可以通过${属性名称}来调用属性值]

3)使用随机数:可以通过${random.int}类似的写法赋予属性随机数的特性

4)通过命令行设置属性值

5)多环境配置
application.properties文件和pom.xml文件的区别

1)文件存放位置不同
pom.xml文件是存放在整个项目的根目录之下,和src文件夹同级;
application.properties文件是放在:项目名/src/main/resources 文件夹中;
2)作用不同
pom.xml文件是所有maven项目必须存在的配置文件,该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等;(maven是一个跨平台的项目管理工具。)

application.properties文件是一个springBoot框架下的核心配置文件
(SpringBoot是一个框架,一种全新的编程规范,他的产生简化了框架的使用,所谓简化是指简化了Spring众多框架中所需的大量且繁琐的配置文件,所以 SpringBoot是一个服务于框架的框架,服务范围是简化配置文件)
文件列表

文件列表示例代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
     
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <title>下载列表</title>
      </head>
      <body>
        <c:forEach var="me" items="${map}">
        <c:url value="/servlet/DownloadServlet" var="downurl">
        <c:param name="filename" value="${me.key }"></c:param>
        </c:url>
         ${me.value }<a href="${downurl }" >下载</a><br />
        </c:forEach>
        </body>
      </html>

由于文件名可能存在中文名称,这个时候通过超链接提供下载,需要向处理下载的servlet提供下载的文件名。为了避免产生中文乱码问题,需要对文件名进行URL编码,这可以使用jstl标签库中的<c:url>和<c:param>标签,前者将产生一个URL地址,后者为这个地址添加一个filename文件名的参数。具体如下:

    <c:url value="/servlet/DownloadServlet" var="downurl">
                <c:param name="filename" value="${me.key }"></c:param>
            </c:url>

 downurl将是包含一个参数为filename的url访问地址,filename中的中文将会进行URL编码。


借鉴文件上传代码,此处需要产生共文件下载列表页面显示的map容器,因此需要对存放文件的目录进行扫描,将其中所有文件(不是文件夹)的存储文件名和真实文件名加入到map中。下列代码中的makeFileList将完成此功能,他将会将file文件下的所有文件的相关信息加入到map容器中。

ListServlet代码:

    public class ListServlet extends HttpServlet {
     
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            Map<String, String> map = new HashMap<String, String>();
            String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
            File file = new File(savePath);
            if(!file.exists()){
                request.setAttribute("message", "对不起,没有上传文件");
                request.getRequestDispatcher("/message.jsp").forward(request, response);
                return;
            }
            
            makeFileList(file, map);
            request.setAttribute("map", map);
            request.getRequestDispatcher("/downlist.jsp").forward(request, response);
        }
     
        //将file文件夹中所有文件加入到map容器中,map的key值为文件真实存储名,value为文件原始名
        private void makeFileList(File file, Map<String, String> map) {
            // TODO Auto-generated method stub
            File[] files = file.listFiles();
            
            for(int i=0; files!=null && i<files.length; i++){
                if(!files[i].isFile()){
                    makeFileList(files[i], map);
                }else{
                    String saveName = files[i].getName();
                    map.put(saveName, saveName.substring(saveName.indexOf("_")+1));
                }
            }
        }
     
     
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
     
            doGet(request, response);
        }
        由于请求体带过来的url地址的参数是经过url编码的,因此在获取filename参数时,需要先对其进行iso8859-1解码,再用指定编码方式编码。才可以的到期原始值。另外通过filename计算出此文件在服务器存储路劲中的存储目录。要想在下载页面上实现下载保存效果,需要设置response的head头和content-type。

程序实现下载需设置两个响应头:
 设置Content-Type 的值为:application/x-msdownload。Web 服务器需要告诉浏览器其所输出的内容的类型不是普通的文本文件或 HTML 文件,而是一个要保存到本地的下载文件。Web 服务器希望浏览器不直接处理相应的实体内容,而是由用户选择将相应的实体内容保存到一个文件中,这需要设置 Content-Disposition 报头。该报头指定了接收程序处理数据内容的方式,在 HTTP 应用中只有 attachment 是标准方式,attachment 表示要求用户干预。在 attachment 后面还可以指定 filename 参数,该参数是服务器建议浏览器将实体内容保存到文件中的文件名称。在设置 Content-Dispostion 之前一定要指定 Content-Type.

下载示例代码:

    public class DownloadServlet extends HttpServlet {
     
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
            String fileName = (String) request.getParameter("filename");
            fileName = new String(fileName.getBytes("iso8859-1"), "utf-8");//filename参数值进行了url编码,需要先进行iso8859-1解码,在进行utf-8编码
            System.out.println(fileName);
            String realName = fileName.substring(fileName.indexOf("_") + 1);//获取文件的真实名称
            savePath = getSavePath(savePath, fileName);//通过文件打散算法,利用文件存储名获取其在服务器中的存储路径
            
            File f = new File(savePath+"\\"+fileName);
            if(!f.exists()){
                request.setAttribute("message", "文件已经被删除");
                request.getRequestDispatcher("/message.jsp").forward(request, response);
                return;
            }
            
            response.setContentType("application/x-msdownload");
            response.setHeader("content-disposition", "attachment;filename="+realName);//实现文件下载保存效果
            FileInputStream in = new FileInputStream(savePath+"\\"+fileName); //读取文件并写入到浏览器中
            OutputStream out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int len=0;
            while((len=in.read(buf))>0){
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
     
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
     
            doGet(request, response);
        }
     
        // 文件打散问题,解决一个文件下文件过多的问题
        private String getSavePath(String savePath, String fileName) {
            if (fileName == null || savePath == null) {
                return null;
            }
     
            int hCode = fileName.hashCode();
            int dir1 = hCode & 0x0f;
            int dir2 = (hCode & 0xf0) >> 4;
     
            String dir = savePath + "\\" + dir1 + "\\" + dir2;
     
            // 文件要是不存在,创建此文件
            File f = new File(dir);
            if (!f.exists()) {
                return null;
            }
     
            return dir;
        }
    }

注意细节:

1.提交request请求消息体带有的参数含有中文数据时,需要对其进行URL编码

2.对URL进行编码的,不能直接取出其值,需要进行iso8859-1解码,在用指定的编码方式编码

3.需要设置response head的content-disposition头,值为attachment;filename=文件名

response.setHeader("content-disposition", "attachment;filename="+realName);//实现文件下载保存效果


4.设置content-type值为application/x-msdownload

response.setContentType("application/x-msdownload");

 需注意的是,在设置content-disposition之前一定需要先设置content-type

5.解析文件时需要通过文件名来获取其存储路径
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧日的群星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值