admin管理员组文章数量:1130349
@GetMapping("file/download")
public void downloadDevtool(HttpServletRequest request, HttpServletResponse response) throws IOException {
String property = System.getProperty("user.dir");
String path = property + "/src/main/resources/file/text.txt";
File file = new File(path);
String filename = path.substring(path.lastIndexOf("/")+1);
response.setContentType("application/x-download");
response.setHeader("content-Disposition", "attachment;filename=" + filename);
InputStream in = null;
try {
in = new FileInputStream(file);
int len = 0;
byte buffer[] = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@GetMapping("file/download")
public void downloadDevtool(HttpServletRequest request, HttpServletResponse response) throws IOException {
String property = System.getProperty("user.dir");
String path = property + "/src/main/resources/file/text.txt";
File file = new File(path);
String filename = path.substring(path.lastIndexOf("/")+1);
response.setContentType("application/x-download");
response.setHeader("content-Disposition", "attachment;filename=" + filename);
InputStream in = null;
try {
in = new FileInputStream(file);
int len = 0;
byte buffer[] = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本文标签: 浏览器文件SpringBoot
版权声明:本文标题:springboot直接从浏览器下载文件 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1763895159a2970328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论