//通过服务端
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
Java
public void down(String fileId,HttpServletResponse response) {
//文件路径存在db中
String path = "D:/test/1.xlsx";
File file = new File(path);
//创建文件输入流
try {
FileInputStream in = new FileInputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
//创建输出流
OutputStream out = null;
try {
out = response.getOutputStream();
response.reset();
String fileSuffix = null;
if(null != path||path.length!=0){
fileSuffix=path.substring(path.lastIndexOf(".")+1);
}
response.setContentType(fileSuffix);
String fileName = path.substring(path.lastIndexOf("\\")+1);
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
} catch (IOException e) {
e.printStackTrace();
}
downloadFile(file, out);
}
public void downloadFile(File file, OutputStream output){
FileInputStream fileInputStream = null;
BufferedInputStream inputStream = null;
try {
fileInputStream = new FileInputStream(file);
inputStream = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[8192];//1024*8
int i;
while ((i = inputStream.read(buffer)) != -1) {
output.write(buffer,0,i);
}./*欢迎加入java交流Q君样:909038429一起吹水聊天
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
if (fileInputStream != null)
fileInputStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
最新2020整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程,spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:909038429
/./*欢迎加入java交流Q君样:909038429一起吹水聊天