1. 將文件以流的形式一次性讀取到內(nèi)存,通過響應(yīng)輸出流輸出到前端
/**
* @param path 想要下載的文件的路徑
* @param response
* @功能描述 下載文件:
*/
@RequestMapping("/download")
public void download(String path, HttpServletResponse response) {
try {
// path是指想要下載的文件的路徑
File file = new File(path);
log.info(file.getPath());
// 獲取文件名
String filename = file.getName();
// 獲取文件后綴名
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
log.info("文件后綴名:" + ext);
// 將文件寫入輸入流
FileInputStream fileInputStream = new FileInputStream(file);
InputStream fis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設(shè)置response的Header
response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用:告知瀏覽器以何種方式顯示響應(yīng)返回的文件,用瀏覽器打開還是以附件的形式下載到本地保存
//attachment表示以附件方式下載 inline表示在線打開 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默認(rèn)名稱,因?yàn)榫W(wǎng)絡(luò)傳輸只支持URL編碼的相關(guān)支付,因此需要將文件名URL編碼后進(jìn)行傳輸,前端收到后需要反編碼才能獲取到真正的名稱
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 告知瀏覽器文件的大小
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
1.1 下載文件
@RequestMapping("getFile")
@ResponseBody
public void getFile(HttpServletResponse response) throws Exception{
File readFile = new File("/home/ssx/Music/CloudMusic/夜曲-周杰倫.flac");
//字節(jié)流-用于讀文件 這里只是demo用的非緩沖流。實(shí)際項(xiàng)目可以用BufferedInputStream。 此功能是讀取圖片,所以用的字節(jié)流。如果是文本的話可以用字符流效率高,具體類看下面注釋
// BufferedReader bufferedReader = new BufferedReader(new FileReader(readFile));//字符流
FileInputStream fileInputStream = new FileInputStream(readFile);//字節(jié)流
//設(shè)置文件下載方式
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("夜曲-周杰倫.flac","utf-8"));
//獲取servlet響應(yīng)輸出字節(jié)流
// PrintWriter writer = response.getWriter();//字符流
ServletOutputStream outputStream = response.getOutputStream(); //字節(jié)流
//流數(shù)據(jù)交換,每次交換10k數(shù)據(jù)大小 (1024k = 1m)
byte[] bytes = new byte[1024*10];
int read;
do {
read = fileInputStream.read(bytes);
outputStream.write(bytes,0,read);
}while (-1 != read);
//關(guān)閉資源
IOUtils.closeQuietly(fileInputStream);
IOUtils.closeQuietly(outputStream);
}
2. 將輸入流中的數(shù)據(jù)循環(huán)寫入到響應(yīng)輸出流中,而不是一次性讀取到內(nèi)存,通過響應(yīng)輸出流輸出到前端
/**
* @param path 指想要下載的文件的路徑
* @param response
* @功能描述 下載文件:將輸入流中的數(shù)據(jù)循環(huán)寫入到響應(yīng)輸出流中,而不是一次性讀取到內(nèi)存
*/
@RequestMapping("/downloadLocal")
public void downloadLocal(String path, HttpServletResponse response) throws IOException {
// 讀到流中
InputStream inputStream = new FileInputStream(path);// 文件的存放路徑
response.reset();
response.setContentType("application/octet-stream");
String filename = new File(path).getName();
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
ServletOutputStream outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int len;
//從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲在緩沖區(qū)字節(jié)數(shù)組中,讀到末尾返回-1
while ((len = inputStream.read(b)) > 0) {
outputStream.write(b, 0, len);
}
inputStream.close();
}
3. 下載網(wǎng)絡(luò)文件到本地
/**
* @param path 下載后的文件路徑和名稱
* @param netAddress 文件所在網(wǎng)絡(luò)地址
* @功能描述 網(wǎng)絡(luò)文件下載到服務(wù)器本地
*/
@RequestMapping("/netDownloadLocal")
public void downloadNet(String netAddress, String path) throws IOException {
URL url = new URL(netAddress);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(path);
int bytesum = 0;
int byteread;
byte[] buffer = new byte[1024];
while ((byteread = inputStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fileOutputStream.write(buffer, 0, byteread);
}
fileOutputStream.close();
}
4. 網(wǎng)絡(luò)文件獲取到服務(wù)器后,經(jīng)服務(wù)器處理后響應(yīng)給前端
/**
* @param netAddress
* @param filename
* @param isOnLine
* @param response
* @功能描述 網(wǎng)絡(luò)文件獲取到服務(wù)器后,經(jīng)服務(wù)器處理后響應(yīng)給前端
*/
@RequestMapping("/netDownLoadNet")
public void netDownLoadNet(String netAddress, String filename, boolean isOnLine, HttpServletResponse response) throws Exception {
URL url = new URL(netAddress);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
response.reset();
response.setContentType(conn.getContentType());
if (isOnLine) {
// 在線打開方式 文件名應(yīng)該編碼成UTF-8
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(filename, "UTF-8"));
} else {
//純下載方式 文件名應(yīng)該編碼成UTF-8
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
}
byte[] buffer = new byte[1024];
int len;
OutputStream outputStream = response.getOutputStream();
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-738900.html
文章來源:http://www.zghlxwxcb.cn/news/detail-738900.html
到了這里,關(guān)于SpringBoot實(shí)現(xiàn)文件下載的多種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!