從Springboot項(xiàng)目中下載文件的具體過程
最近在做一個(gè)臨時(shí)的項(xiàng)目,APP端在檢測到程序有更新時(shí),需要去后臺(tái)下載新的安裝包。具體過程如下:
controller層:
/**
* 下載app
* @param response
*/
@RequestMapping("downApp")
@ResponseBody
public void Download(HttpServletResponse response) {
String fileName ="wuye.apk";
String result = FileUtil.downloadFile(response, fileName);
log.info("app包下載結(jié)果:",result);
}
工具類:
public class FileUtil {
public static String downloadFile(HttpServletResponse response, String fileName) {
File path =null;
response.setHeader("content-type","application/octet-stream");
response.setContentType("application/octet-stream");
try {
response.setHeader("Content-Disposition","attachment;filename=" + java.net.URLEncoder.encode(fileName,"UTF-8"));
}catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
byte[] buff =new byte[1024];
BufferedInputStream bis =null;
OutputStream os =null;
try {
path =new File(ResourceUtils.getURL("classpath:").getPath());
os = response.getOutputStream();
bis =new BufferedInputStream(new FileInputStream(new File(path +"/doc/" + fileName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff,0, buff.length);
os.flush();
i = bis.read(buff);
}
}catch (FileNotFoundException e1) {
//e1.getMessage()+"系統(tǒng)找不到指定的文件";
return "系統(tǒng)找不到指定的文件";
}catch (IOException e) {
e.printStackTrace();
}finally {
if (bis !=null) {
try {
bis.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return "success";
}
訪問:http://127.0.0.1:8081/ymd/downApp 文件就下載下來了,本方法借鑒了 網(wǎng)絡(luò)上的一些文章
到此這篇關(guān)于從Springboot項(xiàng)目中下載文件的文章就介紹到這了,更多相關(guān)Springboot項(xiàng)目下載文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 2021.1 首個(gè) Beta 版本發(fā)布
這篇文章主要介紹了IntelliJ IDEA 2021.1 首個(gè) Beta 版本發(fā)布,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換
這篇文章主要介紹了JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換,如果在瀏覽器端JSON是list則轉(zhuǎn)為string結(jié)構(gòu)來處理,需要的朋友可以參考下2016-04-04
IDEA巧用Postfix Completion讓碼速起飛(小技巧)
這篇文章主要介紹了IDEA巧用Postfix Completion讓碼速起飛,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java中的SynchronousQueue隊(duì)列詳解
這篇文章主要介紹了Java中的SynchronousQueue隊(duì)列詳解,SynchronousQueue是BlockingQueue的一種,所以SynchronousQueue是線程安全的,SynchronousQueue和其他的BlockingQueue不同的是SynchronousQueue的capacity是0,需要的朋友可以參考下2023-12-12
spring boot配置ssl(多cer格式)超詳細(xì)教程
這篇文章主要介紹了spring boot配置ssl(多cer格式)超詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11

