zoukankan      html  css  js  c++  java
  • 07springmvc文件下载

    文件下载
    ---------------------------------------------------
    1)、普通下载
    不安全,
    下载人员可以直接看到下载地址,可以盗链接
    随时可以下载

    <a href="soft/a.rar">软件下载</a>

    html5 新属性
    <a href="soft/a.rar" download>软件下载</a>
    <a href="imgs/girl.jpg" download>美女图片下载</a>
    <a href="/user.jsp" download>jsp程序下载</a>

    2)、安全下载
    必须登录才可以下载
    下载人,从来不知道文件在服务器那个地方

    springmvc 框架内置文件安全下载
    @RequestMapping("/down")
    public ResponseEntity<byte[]> down(HttpServletRequest req) throws IOException {
    String f = req.getServletContext().getRealPath("/imgs/") + "girl.jpg";
    File file = new File(f);
    byte[] body = null;
    InputStream is = new FileInputStream(file);
    body = new byte[is.available()];
    is.read(body);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "attchement;filename=" + file.getName());
    HttpStatus statusCode = HttpStatus.OK;
    ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
    return entity;
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpServletRequest req, HttpServletResponse res) throws IOException {
    ResponseEntity<byte[]> entity = null;
    HttpSession session = req.getSession();
    if(session.getAttribute("member")==null){
    res.setCharacterEncoding("utf-8");
    PrintWriter out = res.getWriter();
    out.print("请登录,登录后才可以下载...");
    out.flush();
    out.close();
    }else {
    String name = req.getParameter("name");
    byte[] body = null;
    String f = req.getServletContext().getRealPath("/download/") + name;
    File file = new File(f);
    InputStream is = new FileInputStream(file);
    body = new byte[is.available()];
    is.read(body);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "attchement;filename=" + file.getName());
    HttpStatus statusCode = HttpStatus.OK;
    entity = new ResponseEntity<byte[]>(body, headers, statusCode);
    }
    return entity;
    }

    3)、java web通用的下载实现
    @RequestMapping("/mydown") @ResponseBody
    public void mydown(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String fileName = req.getParameter("name");
    if (fileName == null) {
    resp.setCharacterEncoding("utf-8");
    PrintWriter out = resp.getWriter();
    out.print("没有下载的文件...");
    out.flush();
    out.close();
    } else {
    resp.setContentType("application/force-download");// 设置强制下载不打开

    String fn = new SimpleDateFormat("yyyyMMddhhmmssS").format(new Date())+fileName.substring(fileName.lastIndexOf("."));
    resp.addHeader("Content-Disposition", "attachment;fileName=" + fn);// 设置文件名

    String path = req.getServletContext().getRealPath("/download/"); //文件下载源路径

    File file = new File(path, fileName);
    byte[] buf = new byte[10240];
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    OutputStream os = resp.getOutputStream();
    int i = bis.read(buf);
    while (i != -1) {
    os.write(buf, 0, i);
    i = bis.read(buf);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (bis != null) {
    try {
    bis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (fis != null) {
    try {
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }

    使用方法
    <h3>jsp自己实现下载</h3>
    <a href="mydown?name=girl.jpg" download>girl-mydown</a>&nbsp;&nbsp;
    <a href="mydown?name=m.jsp" download>jsp</a>
    <a href="mydown?id=60">文档下载</a>

    怕什么真理无穷,进一步有一步的欢喜
  • 相关阅读:
    nyoj256-C小加之级数求和
    nyoj254-编号统计
    nyoj286-动物统计
    最长回文子串——manacher
    动态规划:Codeforces Round #427 (Div. 2) C Star sky
    水题:51Nod1432-独木舟
    水题:HDU1716-排列2
    水题:CF16C-Monitor
    数学基础:HUD1124-Factorial(N!末尾0的个数)
    并查集:POJ1182-食物链(并查集比较高端的应用)
  • 原文地址:https://www.cnblogs.com/Mkady/p/7200845.html
Copyright © 2011-2022 走看看