Java下載文件的四種方式詳細(xì)代碼
1.以流的方式下載
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下載的文件的路徑。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后綴名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下載文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設(shè)置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
2.下載本地文件
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
// 下載本地文件
String fileName = "Operator.doc".toString(); // 文件的默認(rèn)保存名
// 讀到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
// 設(shè)置輸出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循環(huán)取出流中的數(shù)據(jù)
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3.下載網(wǎng)絡(luò)文件
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
// 下載網(wǎng)絡(luò)文件
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
4.支持在線打開(kāi)的方式
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在線打開(kāi)方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名應(yīng)該編碼成UTF-8
} else { // 純下載方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
到此這篇關(guān)于Java下載文件的四種方式詳細(xì)代碼的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot連接Hive實(shí)現(xiàn)自助取數(shù)的示例
這篇文章主要介紹了SpringBoot連接Hive實(shí)現(xiàn)自助取數(shù)的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
每日六道java新手入門(mén)面試題,通往自由的道路--多線程
這篇文章主要為大家分享了最有價(jià)值的6道多線程面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對(duì)hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下2021-06-06
springboot健康檢查監(jiān)控全過(guò)程
文章介紹了Spring Boot如何使用Actuator和Micrometer進(jìn)行健康檢查和監(jiān)控,通過(guò)配置和自定義健康指示器,開(kāi)發(fā)者可以實(shí)時(shí)監(jiān)控應(yīng)用組件的狀態(tài),Micrometer支持多種監(jiān)控系統(tǒng),如Prometheus,而Grafana則用于可視化監(jiān)控?cái)?shù)據(jù),文章還提供了配置示例和常見(jiàn)問(wèn)題解決方案2025-01-01
MyBatis找不到mapper文件的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis找不到mapper文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
SpringBoot中自定義注解實(shí)現(xiàn)參數(shù)非空校驗(yàn)的示例
這篇文章主要介紹了SpringBoot中自定義注解實(shí)現(xiàn)參數(shù)非空校驗(yàn),幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-11-11
Java動(dòng)態(tài)代理和AOP應(yīng)用示例
這篇文章主要介紹了Java動(dòng)態(tài)代理和AOP應(yīng)用,結(jié)合實(shí)例形式分析了java動(dòng)態(tài)代理在AOP面向切面編程中的相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-07-07
定義hashcode時(shí)使用31系數(shù)的原因
這篇文章主要介紹了定義hashcode時(shí)使用31系數(shù)的原因,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Springboot實(shí)現(xiàn)對(duì)配置文件中的明文密碼加密詳解
我們?cè)赟pringBoot項(xiàng)目當(dāng)中,會(huì)把數(shù)據(jù)庫(kù)的用戶名密碼等配置直接放在yaml或者properties文件中,這樣維護(hù)數(shù)據(jù)庫(kù)的密碼等敏感信息顯然是有一定風(fēng)險(xiǎn)的。所以本文為大家整理了對(duì)配置文件中的明文密碼加密的方法,希望對(duì)大家有所幫助2023-03-03

