如何使用ByteArrayOutputStream下載文件
使用ByteArrayOutputStream下載文件
//文件名稱
String filepath = ServletActionContext.getServletContext()
.getRealPath(farmerQrCode.getQrCodeUrl());
File file = new File(filepath);
String fileName = new Date().getTime()+".png";
//設(shè)置請求信息
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(response.getContentType());
response.setHeader("Content-disposition",
"attachment; filename="+fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
FileInputStream inputStream = new FileInputStream(file);
byte [] buffer = new byte[3];
while((len = inputStream.read(buffer)) != -1)
{
baos.write(buffer, 0, len);
}
byte[] bytes = baos.toByteArray();
response.setHeader("Content-Length", String.valueOf(bytes.length));
BufferedOutputStream bos = null;
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(bytes);
bos.close();
baos.close();
使用POI導(dǎo)出數(shù)據(jù),然后將其下載
//此處將HSSFWorkbook wb處理好,然后最后要導(dǎo)出文件時(shí)加上此代碼。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.setContentType(response.getContentType());
response.setHeader("Content-disposition",
"attachment; filename=monthPayment.xls");
wb.write(baos);
byte[] bytes = baos.toByteArray();
response.setHeader("Content-Length", String.valueOf(bytes.length));
BufferedOutputStream bos = null;
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(bytes);
bos.close();
baos.close();
1、使用inputStream.read(buffer)方法分段的把txt文本中的內(nèi)容寫入buffer數(shù)組。
這里為buffer數(shù)組指定了長度為3,所以“hello world!”這組長度為11的數(shù)據(jù)會(huì)被分成4次寫入到buffer數(shù)組中。
當(dāng)inputStream.read(buffer)把數(shù)據(jù)都寫入到buffer數(shù)組之后,它最后還會(huì)返回一次len為-1的值,代表數(shù)據(jù)完全讀完。
2、使用outStream.write(buffer, 0, len)方法,在while循環(huán)體內(nèi)把每次寫入到buffer數(shù)組的值再次疊加寫入到內(nèi)存緩沖區(qū)中。
3、使用outStream.toByteArray()方法把內(nèi)存緩沖區(qū)中的數(shù)據(jù)流轉(zhuǎn)換成字節(jié)數(shù)組。
4、最后把字符數(shù)組轉(zhuǎn)換成字符串進(jìn)行返回return new String(data)。
使用ByteArrayOutputStream解決IO亂碼
說下經(jīng)過
今天在用s3接口做ceph儲(chǔ)存的時(shí)候,要實(shí)現(xiàn)一個(gè)io下載的接口。
需要把InputStream轉(zhuǎn)成byte[],一開始,是的寫法是這樣的:
byte[] buf = new byte[(int) fileSize];
InputStream in = ossObject.getObjectContent();
try {
for (int n = 0; n != -1; ) {
n = in.read(buf, 0, buf.length);
}
} catch (IOException e) {
log.error(e.getMessage());
} finally {
try {
in.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
可是下載的文件稍大一些,就會(huì)出現(xiàn)亂碼。
于是換了網(wǎng)上推薦的,使用byte緩存的方法,來實(shí)現(xiàn)InputStream轉(zhuǎn)成byte[]:
private static byte[] inputToByte(InputStream inStream, int fileSize) throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[fileSize];
int rc;
while ((rc = inStream.read(buff, 0, fileSize)) > 0) {
swapStream.write(buff, 0, rc);
}
return swapStream.toByteArray();
}
亂碼的情況就解決了!
小結(jié)一下
IO這塊不是很熟悉,盡量不要用原生的方法去寫,而應(yīng)該使用JDK封裝好的方法去實(shí)現(xiàn)。避免出現(xiàn)一些意料之外的問題。
PS:至于上面那段代碼為什么會(huì)出現(xiàn)亂碼,暫時(shí)還未研究出來。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Kubernetes和Docker部署Java微服務(wù)詳細(xì)代碼
Java微服務(wù)項(xiàng)目是一種基于Java技術(shù)棧的分布式系統(tǒng)開發(fā)方式,下面這篇文章主要給大家介紹了關(guān)于使用Kubernetes和Docker部署Java微服務(wù)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
Java高性能本地緩存框架Caffeine的實(shí)現(xiàn)
本文主要介紹了Java高性能本地緩存框架Caffeine的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
java實(shí)現(xiàn)給出分?jǐn)?shù)數(shù)組得到對應(yīng)名次數(shù)組的方法
這篇文章主要介紹了java實(shí)現(xiàn)給出分?jǐn)?shù)數(shù)組得到對應(yīng)名次數(shù)組的方法,涉及java針對數(shù)組的遍歷、排序及運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2015-07-07
ZooKeeper入門教程二在單機(jī)和集群環(huán)境下的安裝搭建及使用
本文是ZooKeeper入門系列教程,涵蓋ZooKeeper的安裝使及單機(jī)集群環(huán)境搭建,通過實(shí)例和大量圖表,結(jié)合實(shí)戰(zhàn),幫助學(xué)習(xí)者理解和運(yùn)用,有需要的朋友可以借鑒參考下2022-01-01
Maven 搭建SpringMVC+Hibernate項(xiàng)目詳解
本文主要介紹Maven 搭建SpringMVC+Hibernate的知識(shí),這里整理了詳細(xì)的資料,并附示例代碼,有興趣的小伙伴可以參考下2016-09-09
Springboot調(diào)整接口響應(yīng)返回時(shí)長詳解(解決響應(yīng)超時(shí)問題)
當(dāng)后端對于數(shù)據(jù)量較大的處理或是某些耗時(shí)的操作時(shí),需要先對請求接口的請求進(jìn)行響應(yīng),下面這篇文章主要給大家介紹了關(guān)于Springboot調(diào)整接口響應(yīng)返回時(shí)長(解決響應(yīng)超時(shí)問題)的相關(guān)資料,需要的朋友可以參考下2023-01-01

