最近做产品时有个下载视频的功能,用了两种方法,第一种方法是直接通过浏览器(有两种方式),其中直接通过浏览器下载-----第一种到最后显示失败,如下显示:
直接通过浏览器下载-----第二种方法下载成功,如下显示:
本然感觉两种方法差不多啊,求路过的各位大佬赐教
直接通过浏览器下载-----第一种:
public void downloadVideoById(Integer id,HttpServletResponse response) throws IOException {
BufferedInputStream bis = null;
OutputStream os = null;
BehaviorDemoVideo video = behaviorDemoVideoMapper.findDemoVideoById(id);
String videoPath = ConstansPropertity.videoPath;//路径
String videoName = video.getFile();//文件名
File file = new File(videoPath, videoName);
if(file.exists()){
try {
String showName = video.getName()+video.getFile().substring(video.getFile().lastIndexOf("."));
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/"+video.getFile().substring(video.getFile().lastIndexOf("."))+1);
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(showName,"UTF-8"));//下载时浏览器显示的名称
byte[] buff = new byte[1024];
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File(videoPath+videoName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
logger.error(e.getMessage());
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}
}
直接通过浏览器下载-----第二种:
使用spring框架中的FileCopyUtils工具
public void downloadVideoById(Integer id,HttpServletResponse response) throws IOException {
BehaviorDemoVideo video = behaviorDemoVideoMapper.findDemoVideoById(id);
String videoPath = ConstansPropertity.videoPath;//路劲
String videoName = video.getFile();//文件名
File file = new File(videoPath, videoName);
if(file.exists()){
String showName = video.getName()+video.getFile().substring(video.getFile().lastIndexOf("."));
//获取输入流对象(用于读文件)
FileInputStream fis = new FileInputStream(new File(videoPath, videoName));
//动态设置响应类型,根据前台传递文件类型设置响应类型
response.setContentType("application/"+video.getFile().substring(video.getFile().lastIndexOf("."))+1);
//设置响应头,attachment表示以附件的形式下载,inline表示在线打开
response.setHeader("content-disposition","attachment;fileName="+URLEncoder.encode(showName,"UTF-8"));//下载时浏览器显示的名称
//获取输出流对象(用于写文件)
ServletOutputStream os = response.getOutputStream();
//下载文件,使用spring框架中的FileCopyUtils工具
FileCopyUtils.copy(fis,os);
}
}
请各位大佬指教!
第二种(直接下载到本地)
/**
* 根据id下载演示视频
* @param id
* @return
*/
@Override
public StateMsg downloadVideoById(Integer id) {
BehaviorDemoVideo video = behaviorDemoVideoMapper.findDemoVideoById(id);
String videoPath = ConstansPropertity.videoPath;
String videoName = video.getFile();
// 待下载的视频
File file = new File(videoPath, videoName);
String showName = null;
if(file.exists()){
try {
showName = video.getName()+video.getFile().substring(video.getFile().lastIndexOf("."));// 截取待下载视频后缀名并拼接新的视频名
//获取输入流对象(用于读文件)
FileInputStream in = new FileInputStream(file);
// 文件保存路劲
File dir = new File(ConstansPropertity.fileTemp);
if(! dir.exists()){
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(ConstansPropertity.fileTemp+showName);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
fos.flush();
}
in.close();
fos.close();
} catch (FileNotFoundException e) {
logger.error(e.getMessage());
return new StateMsg(false, "文件未找到!");
} catch (IOException e) {
logger.error(e.getMessage());
return new StateMsg(false, "下载错误!");
}
}
// new StateMsg(true, video.getName()+video.getFile().substring(video.getFile().lastIndexOf(".")))
return new StateMsg(true, ConstansPropertity.ServerAddressUrl+Constans.RESOURSE_TEMP+showName);
}
}