欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC實現(xiàn)文件下載功能

 更新時間:2017年03月09日 11:38:17   作者:MrSaber  
這篇文章主要為大家詳細(xì)介紹了SpringMVC實現(xiàn)文件下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了SpringMVC文件下載的具體代碼,供大家參考,具體內(nèi)容如下

兩個案例

  1.為登錄用戶提供下載服務(wù)。

  2.阻止僅通過輸入網(wǎng)址即可獲取下載。

文件下載概覽

  為了將文件發(fā)送給瀏覽器,我們需要在控制器中完成以下操作:

  • 對請求處理方法使用void返回類型,并且在方法中添加HttpServletResponse參數(shù)。
  • 將響應(yīng)的內(nèi)容類型設(shè)為文件的內(nèi)容類型。Content-Type標(biāo)題在某個實體的body中定義數(shù)據(jù)的類型,并包含媒體類型和子類型標(biāo)識符。如果不清楚內(nèi)容類型,并且希望瀏覽器失始終顯示保存對話框,則將它設(shè)為APPLICATION/OCTET-STREAM。這個值時不區(qū)分大小寫的。
  • 添加一個名為Content-Disposition的HTTP響應(yīng)標(biāo)題,并賦值attachment;filename=fileName,這里的fileName是默認(rèn)的文件名。

案例1:為登錄用戶提供下載服務(wù)

Domain類

package domain;
public class Login {
 private String username;
 private String password;

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }
}

Controller控制器

package controller;

import domain.Login;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;

@Controller
public class ResourceController {
 private static final Log logger = LogFactory.getLog(ResourceController.class);

 @RequestMapping(value = "/login")
 public String login(@ModelAttribute Login login, HttpSession session, Model model)
 {
  model.addAttribute("login",new Login());
  if("ms".equals(login.getUsername())&&"123".equals(login.getPassword()))
  {
   session.setAttribute("loggedIn",Boolean.TRUE);
   return "Main";
  }
  else
  {
   return "LoginForm";
  }
 }

 @RequestMapping(value = "/resource_download")
 public String downloadResource(HttpSession session, HttpServletRequest request, HttpServletResponse response)
 {
  if(session==null||session.getAttribute("loggedIn")==null)
  {
  return "LoginForm";
  }
  String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/data");
  File file = new File(dataDirectory,"Blog.zip");
  if(file.exists())
  {
   response.setContentType("application/octet-stream");
   response.addHeader("Content-Disposition","attachment;filename=Blog.zip");
   byte[] buffer = new byte[1024];
   FileInputStream fis =null;
   BufferedInputStream bis =null;
   try {
    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    OutputStream os =response.getOutputStream();
    int i =bis.read(buffer);
    while (i!=-1) {
     os.write(buffer, 0, i);
     i=bis.read(buffer);
    }
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }finally {
    try {
     bis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
    try {
     fis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }

  }
  return null;
 }
}


編寫視圖

Main.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>DownPage</title>
</head>
<body>
 <h4>點(diǎn)擊鏈接下載文件</h4>
 <p>
  <a href="resource_download" rel="external nofollow" >Down</a>
 </p>
</body>
</html>

LoginForm.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>登錄頁面</title>
</head>
<body>
 <form:form commandName="login" action="login" method="post">
  賬號: <br>
  <form:input path="username" cssErrorClass="error" id="username"/>
  <br> 密碼: <br>
  <form:input path="password" cssErrorClass="error" id="password"/>
  <br> <input type="submit" value="提交">
 </form:form>
</body>
</html>

案例2:阻止僅通過輸入網(wǎng)址即可獲取下載

 @RequestMapping(value = "/resource_download")
 public String downloadResource(HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestHeader String refuer
 ){
  if(refer==null) //通過判斷refer來瀏覽器輸入網(wǎng)址就能下載圖片的情況
  {
    return "LoginForm";
  }
  String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/data");
  File file = new File(dataDirectory,"Blog.zip");
  if(file.exists())
  {
   response.setContentType("application/octet-stream");
   response.addHeader("Content-Disposition","attachment;filename=Blog.zip");
   byte[] buffer = new byte[1024];
   FileInputStream fis =null;
   BufferedInputStream bis =null;
   try {
    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    OutputStream os =response.getOutputStream();
    int i =bis.read(buffer);
    while (i!=-1) {
     os.write(buffer, 0, i);
     i=bis.read(buffer);
    }
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }finally {
    try {
     bis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
    try {
     fis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }

  }
  return null;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • 使用jsoup解析html的table中的文本信息實例

    使用jsoup解析html的table中的文本信息實例

    今天小編就為大家分享一篇使用jsoup解析html的table中的文本信息實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 解析Spring中面向切面編程

    解析Spring中面向切面編程

    如果說 IoC 是 Spring 的核心,那么面向切面編程就是 Spring 最為重要的功能之一了,在數(shù)據(jù)庫事務(wù)中切面編程被廣泛使用
    2021-06-06
  • Java中自己如何實現(xiàn)log2(N)

    Java中自己如何實現(xiàn)log2(N)

    這篇文章主要介紹了Java中自己實現(xiàn)log2(N)的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • IO流概述分類字節(jié)流寫數(shù)據(jù)三種方式及問題分析

    IO流概述分類字節(jié)流寫數(shù)據(jù)三種方式及問題分析

    這篇文章主要為大家介紹了IO流概述分類字節(jié)流寫數(shù)據(jù)三種方式及寫數(shù)據(jù)問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 使用@JsonFormat和@DateTimeFormat對Date格式化操作

    使用@JsonFormat和@DateTimeFormat對Date格式化操作

    這篇文章主要介紹了使用@JsonFormat和@DateTimeFormat對Date格式化操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 如何使用Java調(diào)用Spark集群

    如何使用Java調(diào)用Spark集群

    這篇文章主要介紹了如何使用Java調(diào)用Spark集群,我搭建的Spark集群的版本是2.4.4,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • SpringCloud?openfeign聲明式服務(wù)調(diào)用實現(xiàn)方法介紹

    SpringCloud?openfeign聲明式服務(wù)調(diào)用實現(xiàn)方法介紹

    在springcloud中,openfeign是取代了feign作為負(fù)載均衡組件的,feign最早是netflix提供的,他是一個輕量級的支持RESTful的http服務(wù)調(diào)用框架,內(nèi)置了ribbon,而ribbon可以提供負(fù)載均衡機(jī)制,因此feign可以作為一個負(fù)載均衡的遠(yuǎn)程服務(wù)調(diào)用框架使用
    2022-12-12
  • java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解

    java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解

    最近 RocketMQ 剛剛上生產(chǎn)環(huán)境,閑暇之時在這里做一些分享,主要目的是讓初學(xué)者能快速上手RocketMQ,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Java與MySQL時間不一致問題解決

    Java與MySQL時間不一致問題解決

    本文主要介紹了Java與MySQL時間不一致問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法

    String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法

    今天小編就為大家分享一篇String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評論