最近做產品時有個下載視頻的功能,用了兩種方法,第一種方法是直接通過瀏覽器(有兩種方式),其中直接通過瀏覽器下載-----第一種到最後顯示失敗,如下顯示:
直接通過瀏覽器下載-----第二種方法下載成功,如下顯示:
本然感覺兩種方法差不多啊,求路過的各位大佬賜教
直接通過瀏覽器下載-----第一種:
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);
}
}