/**
* 下載下傳模闆
* @param tplName
* @param returnName
* @param response
* @param request
* @throws Exception
*/
@RequestMapping(value = "/downloadTpl")
public void downloadTpl(String tplName,String returnName, HttpServletResponse response,
HttpServletRequest request) throws Exception {
try {
//擷取模闆存放的路徑
String path=request.getSession().getServletContext().getRealPath("/")+"/WEB-INF/ExcelTemplate/";
InputStream is=new FileInputStream(new File(path + tplName));
HSSFWorkbook wb=new HSSFWorkbook(is);
//下載下傳
DownloadUtil dUtil=new DownloadUtil();
ByteArrayOutputStream os=new ByteArrayOutputStream();
wb.write(os);
dUtil.download(os, response, returnName);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
}
(1)其中,需要配置的pom.xml檔案
(2)excel檔案
(3)downloadutil工具類檔案
package com.test.util;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class DownloadUtil {
/**
* @param filePath 要下載下傳的檔案路徑
* @param returnName 傳回的檔案名
* @param response HttpServletResponse
* @param delFlag 是否删除檔案
*/
protected void download(String filePath, String returnName, HttpServletResponse response, boolean delFlag){
this.prototypeDownload(new File(filePath), returnName, response, delFlag);
}
/**
* @param file 要下載下傳的檔案
* @param returnName 傳回的檔案名
* @param response HttpServletResponse
* @param delFlag 是否删除檔案
*/
protected void download(File file,String returnName,HttpServletResponse response,boolean delFlag){
this.prototypeDownload(file, returnName, response, delFlag);
}
/**
* @param file 要下載下傳的檔案
* @param returnName 傳回的檔案名
* @param response HttpServletResponse
* @param delFlag 是否删除檔案
*/
public void prototypeDownload(File file,String returnName,HttpServletResponse response,boolean delFlag){
// 下載下傳檔案
FileInputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
if(!file.exists()) {return;}
response.reset();
//設定響應類型 PDF檔案為"application/pdf",WORD檔案為:"application/msword", EXCEL檔案為:"application/vnd.ms-excel"。
response.setContentType("application/octet-stream;charset=utf-8");
//設定響應的檔案名稱,并轉換成中文編碼
//returnName = URLEncoder.encode(returnName,"UTF-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //儲存的檔案名,必須和頁面編碼一緻,否則亂碼
//attachment作為附件下載下傳;inline用戶端機器有安裝比對程式,則直接打開;注意改變配置,清除緩存,否則可能不能看到效果
response.addHeader("Content-Disposition", "attachment;filename="+returnName);
//将檔案讀入響應流
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
int length = 1024;
int readLength=0;
byte buf[] = new byte[1024];
readLength = inputStream.read(buf, 0, length);
while (readLength != -1) {
outputStream.write(buf, 0, readLength);
readLength = inputStream.read(buf, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//删除原檔案
if(delFlag) {
file.delete();
}
}
}
/**
* by tony 2013-10-17
* @param byteArrayOutputStream 将檔案内容寫入ByteArrayOutputStream
* @param response HttpServletResponse 寫入response
* @param returnName 傳回的檔案名
*/
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //儲存的檔案名,必須和頁面編碼一緻,否則亂碼
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream(); //取得輸出流
byteArrayOutputStream.writeTo(outputstream); //寫到輸出流
byteArrayOutputStream.close(); //關閉
outputstream.flush(); //刷資料
}
}
(4)jsp頁面的調用代碼
//模闆下載下傳
$("#downloadFlowOrderTpl").click(function(){
var tplName = "測試資金流水單模闆.xls";
var returnName = "資金流水單模闆.xls";
window.location.href = "${pageContext.request.contextPath}/uploadCtrl/downloadTpl?tplName="+tplName+"&returnName="+returnName;
});