从Springboot项目中下载文件的具体过程
更新时间:2021年07月27日 09:53:01 作者:文字跳动
最近在做一个临时的项目,APP端在检测到程序有更新时,需要去后台下载新的安装包,接下来通过本文给大家分享从Springboot项目中下载文件的具体过程,感兴趣的朋友一起看看吧
最近在做一个临时的项目,APP端在检测到程序有更新时,需要去后台下载新的安装包。具体过程如下:
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包下载结果:",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()+"系统找不到指定的文件";
return "系统找不到指定的文件";
}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 文件就下载下来了,本方法借鉴了 网络上的一些文章
到此这篇关于从Springboot项目中下载文件的文章就介绍到这了,更多相关Springboot项目下载文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
IntelliJ IDEA 2021.1 首个 Beta 版本发布
这篇文章主要介绍了IntelliJ IDEA 2021.1 首个 Beta 版本发布,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-03-03
JSON的String字符串与Java的List列表对象的相互转换
这篇文章主要介绍了JSON的String字符串与Java的List列表对象的相互转换,如果在浏览器端JSON是list则转为string结构来处理,需要的朋友可以参考下2016-04-04
IDEA巧用Postfix Completion让码速起飞(小技巧)
这篇文章主要介绍了IDEA巧用Postfix Completion让码速起飞,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-08-08


最新评论