欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

專注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站設(shè)計服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)振安免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了超過千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

兩個案例

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

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

文件下載概覽

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

  • 對請求處理方法使用void返回類型,并且在方法中添加HttpServletResponse參數(shù)。
  • 將響應(yīng)的內(nèi)容類型設(shè)為文件的內(nèi)容類型。Content-Type標(biāo)題在某個實(shí)體的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>
 <h5>點(diǎn)擊鏈接下載文件</h5>
 <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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享文章:SpringMVC實(shí)現(xiàn)文件下載功能
網(wǎng)頁網(wǎng)址:http://chinadenli.net/article30/ppcppo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、面包屑導(dǎo)航品牌網(wǎng)站設(shè)計、App設(shè)計、企業(yè)建站品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
日本精品啪啪一区二区三区| 狠狠干狠狠操亚洲综合| 字幕日本欧美一区二区| 高潮少妇高潮久久精品99| 少妇一区二区三区精品| 亚洲精品国男人在线视频| 日韩av生活片一区二区三区| 高清不卡视频在线观看| 欧美午夜国产在线观看| 色婷婷在线精品国自产拍| 国产精品一区二区三区欧美| 亚洲第一视频少妇人妻系列| 九九热精彩视频在线免费| 国产又猛又大又长又粗| 久久久精品区二区三区| 九七人妻一区二区三区| 国产精品一区二区三区欧美 | 后入美臀少妇一区二区| 日韩夫妻午夜性生活视频| 99少妇偷拍视频在线| 91欧美日韩精品在线| 丰满少妇被粗大猛烈进出视频| 国产精品福利一二三区| 日韩欧美三级中文字幕| 国产三级欧美三级日韩三级| 国产av天堂一区二区三区粉嫩| 久久精品a毛片看国产成人| 天海翼高清二区三区在线| 亚洲国产成人精品一区刚刚| 在线观看视频成人午夜| 中文字幕日韩精品人一妻| 亚洲少妇一区二区三区懂色| 欧洲日本亚洲一区二区| 日韩高清一区二区三区四区| 91福利视频日本免费看看| 男女一进一出午夜视频| 人妻久久这里只有精品| 亚洲精品成人综合色在线| 欧美整片精品日韩综合| 国产色一区二区三区精品视频| 国产又猛又黄又粗又爽无遮挡|