最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

兩個(gè)案例

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

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

文件下載概覽

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

  • 對(duì)請(qǐng)求處理方法使用void返回類型,并且在方法中添加HttpServletResponse參數(shù)。
  • 將響應(yīng)的內(nèi)容類型設(shè)為文件的內(nèi)容類型。Content-Type標(biāo)題在某個(gè)實(shí)體的body中定義數(shù)據(jù)的類型,并包含媒體類型和子類型標(biāo)識(shí)符。如果不清楚內(nèi)容類型,并且希望瀏覽器失始終顯示保存對(duì)話框,則將它設(shè)為APPLICATION/OCTET-STREAM。這個(gè)值時(shí)不區(qū)分大小寫的。
  • 添加一個(gè)名為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>登錄頁(yè)面</title>
</head>
<body>
 <form:form commandName="login" action="login" method="post">
  賬號(hào): <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;
 }
}

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

您可能感興趣的文章:

相關(guān)文章

  • Java transient 關(guān)鍵字是干啥的

    Java transient 關(guān)鍵字是干啥的

    這篇文章主要介紹了Java transient 關(guān)鍵字是干啥的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • idea 無法創(chuàng)建Scala class 選項(xiàng)的原因分析及解決辦法匯總

    idea 無法創(chuàng)建Scala class 選項(xiàng)的原因分析及解決辦法匯總

    這篇文章主要介紹了idea 無法創(chuàng)建Scala class 選項(xiàng)的解決辦法匯總,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • centos下安裝openjdk21的詳細(xì)圖文教程

    centos下安裝openjdk21的詳細(xì)圖文教程

    這篇文章主要介紹了centos下安裝openjdk21的相關(guān)資料,文章詳細(xì)介紹了如何手動(dòng)下載、解壓和配置OpenJDK?21,包括下載OpenJDK、上傳到指定目錄、解壓、編輯環(huán)境變量和驗(yàn)證安裝成功的過程,需要的朋友可以參考下
    2024-12-12
  • Java抽象類的構(gòu)造模板模式用法示例

    Java抽象類的構(gòu)造模板模式用法示例

    這篇文章主要介紹了Java抽象類的構(gòu)造模板模式用法,結(jié)合實(shí)例形式分析了java使用抽象類構(gòu)造模板模式相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式

    SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式

    這篇文章主要介紹了SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 基于Eclipce配置Spring Boot過程圖解

    基于Eclipce配置Spring Boot過程圖解

    這篇文章主要介紹了基于Eclipce配置Spring Boot過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • mybatis之批量添加問題

    mybatis之批量添加問題

    這篇文章主要介紹了mybatis之批量添加問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 在?Java?中將Object?轉(zhuǎn)換為?Int的四種方法

    在?Java?中將Object?轉(zhuǎn)換為?Int的四種方法

    這篇文章主要介紹了在Java中如何將?Object?轉(zhuǎn)換為Int,本文研究了在?Java中將Object轉(zhuǎn)換為int的四種不同方法,結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 詳細(xì)了解MVC+proxy

    詳細(xì)了解MVC+proxy

    Java有兩種代理方式,一種是靜態(tài)代理,另一種是動(dòng)態(tài)代理。對(duì)于靜態(tài)代理,其實(shí)就是通過依賴注入,對(duì)對(duì)象進(jìn)行封裝,不讓外部知道實(shí)現(xiàn)的細(xì)節(jié)。很多 API 就是通過這種形式來封裝的
    2021-07-07
  • @Transactional和@DS怎樣在事務(wù)中切換數(shù)據(jù)源

    @Transactional和@DS怎樣在事務(wù)中切換數(shù)據(jù)源

    這篇文章主要介紹了@Transactional和@DS怎樣在事務(wù)中切換數(shù)據(jù)源問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論

成都市| 丰都县| 浪卡子县| 淳化县| 万源市| 千阳县| 武邑县| 漾濞| 宜宾市| 会泽县| 洱源县| 新民市| 萨嘎县| 章丘市| 江都市| 香格里拉县| 榆林市| 日照市| 桦甸市| 鹤山市| 吉隆县| 长宁区| 新泰市| 乌审旗| 南靖县| 永川市| 长顺县| 新丰县| 大城县| 屏东市| 钟山县| 仁怀市| 连平县| 平舆县| 广平县| 红河县| 长岭县| 习水县| 杨浦区| 渝北区| 达拉特旗|