JavaWeb響應(yīng)下載功能實例代碼(包含工具類)
今天通過本文給大家分享是關(guān)于javaweb的響應(yīng)(response)下載
以下是我的Demo:
頁面我就粘主要部分的代碼
<a href = "${pageContext.request.contextPath }/user/courseTab">模板下載</a>
當(dāng)然,現(xiàn)在的項目大家都使用框架,這里我使用的是(SSM),好了,粘代碼
@Controller
@RequestMapping("/user")
public class UploadController {
@RequestMapping(value="/courseTab",method=RequestMethod.GET)
public void courseTab(HttpServletResponse response,HttpServletRequest request) throws IOException{
String path = request.getSession().getServletContext().getRealPath("/courseTab/課表上傳模板.xls");
DownUtil.downMb(response, path, "課表模板"+DateFormat.formatSimple(new Date()));
}
}
這里我使用的DownUtil工具類是我自己寫的,下來我粘到文章中
package org.cxxy.base.cxsc.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
/**
* @Title: DownUtil.java
* @Package org.cxxy.base.cxsc.util
* @Description:文件下載工具類
* @author ChoviWu
* @date 2017年6月18日 下午2:44:17
* @version V1.0
*/
public class DownUtil {
/**
*
* @Description:
* @param @param response
* @param @param url 文件在數(shù)據(jù)庫的路徑
* @param @param base 文件存放的基礎(chǔ)路徑
* @param @param folderPath 上傳所在的文件夾
* @param @return
* @param @throws IOException
* @return int
* @throws
*/
@SuppressWarnings("unused")
public static int downFile(HttpServletResponse response, String url,
Integer down, String base, String folderPath) throws IOException {
// 文件的名稱
String fileName = url.split("/")[1];
System.out.println(fileName);
// 文件的后綴
String last = url.substring(url.lastIndexOf(".") + 1);
System.out.println(last);
// 文件路徑
String downFilePath = base + folderPath + fileName;
Long fileLength = new File(downFilePath).length();// 文件的長度
if (fileLength != 0) {
response.reset();
response.setContentType("application/octet-stream;charset=utf-8"); // 改成輸出excel文件
try {
response.setHeader(
"Content-disposition",
"attachment; filename="
+ new String(fileName.getBytes("utf-8"),
"ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(downFilePath);
bis = new BufferedInputStream(fis);
// 輸出流
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesread;
// 寫文件
while (-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesread);
}
// 跳轉(zhuǎn)的路徑
fis.close();
bis.close();
bos.close();
} catch (FileNotFoundException e) {
System.out.println("File is Not Exsist!");
}
} else {
// 拋異常
response.getWriter()
.write("<script charset='utf-8' type='text/javascript'>alert('該資源不存在!');history.go(-1);</script>");
return down;
}
down++;
return down;
}
/**
*
* @Description: 下載的模板
* @param @param response
* @param @param path 路徑名
* @param @param name 模板名稱
* @param @throws IOException
* @return void
* @throws
*/
@SuppressWarnings("unused")
public static void downMb(HttpServletResponse response, String path,
String name) throws IOException {
Long fileLength = new File(path).length();// 文件的長度
System.out.println("文件的長度:" + fileLength);
if (fileLength != 0) {
response.reset();
response.setContentType("application/octet-stream;charset=utf-8"); // 改成輸出excel文件
try {
response.setHeader(
"Content-disposition",
"attachment; filename="
+ new String(name.getBytes("utf-8"),
"ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
bis = new BufferedInputStream(fis);
// 輸出流
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesread;
// 寫文件
while (-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesread);
}
fis.close();
bis.close();
bos.close();
} catch (FileNotFoundException e) {
System.out.println("File is Not Exsist!");
}
}
}
}
下來,我說一下,調(diào)用的downMb,我們都知道,在服務(wù)器上下載一個文件,
//設(shè)置響應(yīng)頭,控制瀏覽器下載該文件,形參調(diào)的是文件的長度
response.setHeader("Content-Length", String.valueOf(fileLength));
//設(shè)置響應(yīng)類型,設(shè)置輸出流類型
response.setContentType("application/octet-stream;charset=utf-8"); // 改成輸出excel文件
這里我使用的是輸出的Excel文件
接下來就是讀文件,寫文件了,相信學(xué)了java基礎(chǔ)的都會接觸IO吧,這里我就略過
BufferedInputStream bis = null; BufferedOutputStream bos = null;
這里使用的是緩沖流,因其使用的是瀏覽器打開文件的下載
下來就是寫文件了,寫文件也是一貫的套路,先把文件存到buff數(shù)據(jù)緩沖區(qū),然后將buff的數(shù)據(jù)輸出到瀏覽器供用戶查看
byte[] buff = new byte[2048];
int bytesread;
// 寫文件
while (-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesread);
}
當(dāng)讀寫完文件之后,千萬別忘了要關(guān)閉文件流(當(dāng)然,關(guān)閉流的順序也不能變)
fis.close(); bis.close(); bos.close();
以上所述是小編給大家介紹的JavaWeb響應(yīng)下載實例代碼(包含工具類),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- java中添加按鈕并添加響應(yīng)事件的方法(推薦)
- java搭建一個Socket服務(wù)器響應(yīng)多用戶訪問
- javaweb如何實現(xiàn)請求和響應(yīng)
- Java中HttpServletResponse響應(yīng)中文出現(xiàn)亂碼問題
- Java Web請求與響應(yīng)實例詳解
- java常見事件響應(yīng)方法實例匯總
- javasciprt下jquery函數(shù)$.post執(zhí)行無響應(yīng)的解決方法
- JAVA發(fā)送HTTP請求,返回HTTP響應(yīng)內(nèi)容,應(yīng)用及實例代碼
- JavaWeb Refresh響應(yīng)頭代碼實例詳解
相關(guān)文章
Java工作環(huán)境的配置與Eclipse的安裝過程
這篇文章主要介紹了Java工作環(huán)境的配置與Eclipse的安裝過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Java數(shù)組隊列及環(huán)形數(shù)組隊列超詳細(xì)講解
隊列是一個有序列表,可以用數(shù)組和鏈表來實現(xiàn),隊列有一個原則。即:先存入隊列的數(shù)據(jù)要先取出,后存入的要后取出,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
idea如何debug看springsecurity的過濾器順序
這篇文章主要介紹了idea如何debug看springsecurity的過濾器順序,文中通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04

