springboot实现文件下载呢
转自:
下文笔者讲述SpringBoot实现文件下载的方法分享,如下所示:
文件存储在服务器指定位置
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
@RestController
public class Controller {
@GetMapping(value = "/downloadFile", consumes = MediaType.ALL_VALUE)
void downloadFile(final HttpServletResponse response)
throws Exception {
// 获取文件
File file = new File("D:\java265.txt");
//文件名
String fileName = file.getName();
// 清空缓冲区,状态码和响应头(headers)
response.reset();
// 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
response.setContentType("application/octet-stream;charset=utf-8");
// 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
response.setHeader("Content-Disposition", "attachment;fileName=" fileName ";filename*=utf-8''" URLEncoder.encode(fileName,"utf-8"));
// 实现文件下载
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
// 获取字节流
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("Download successfully!");
}
catch (Exception e) {
System.out.println("Download failed!");
}
finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
创建文件数据并下载
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
@RestController
public class Controller {
@GetMapping(value = "/downloadExcel", consumes = MediaType.ALL_VALUE)
void downloadExcel(final HttpServletResponse response)
throws Exception {
String name = "测试." ExcelTypeEnum.XLSX;
// 清空缓冲区,状态码和响应头(headers)
response.reset();
// 设置ContentType,响应内容为文本数据,编码为utf-8,此处设定的编码是文件内容的编码
response.setContentType("text/plain;charset=utf-8");
// 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
response.setHeader("Content-Disposition", "attachment;fileName=" name ";filename*=utf-8''" URLEncoder.encode(name, "utf-8"));
// 响应输出流
OutputStream out = response.getOutputStream();
// 建立excel
ExcelWriter excelWriter = EasyExcel.write(out).build();
// 建立sheet
WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
// 指定sheet并写数据
excelWriter.write(getListString(), writeSheet);
// 不要忘记
excelWriter.finish();
out.flush();
}
// 生成excel内容
List<List<String>> getListString() {
List<List<String>> result = new LinkedList<>();
List<String> data1 = new LinkedList<>();
data1.add("1");data1.add("maomao");data1.add("java265");
result.add(data1);
List<String> data2 = new LinkedList<>();
data1.add("2");data1.add("maomao-2");data1.add("java265-3");
result.add(data2);
List<String> data3 = new LinkedList<>();
data1.add("3");data1.add("maomao-3");data1.add("java265-4");
result.add(data3);
return result;
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhekcc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01