//视频查询
@RequestMapping("/picurTsvideo/{id}")
public void picurTsvideo(@PathVariable("id") String id, HttpServletRequest request,HttpServletResponse response)
throws UnsupportedEncodingException{
//根据ID查询文件对象
Files files=filesService.selectById(id);
//获取文件路劲
String tsfiles =files.getFilepath();
String videoname=tsfiles.substring(tsfiles.lastIndexOf("/")+1,tsfiles.length());
String URLPATH = "/src/main/webapp/static/";
String pp = request.getSession().getServletContext().getRealPath("/") + URLPATH + tsfiles;
File file = new File(pp);
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName="+ java.net.URLEncoder.encode(videoname, "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("success");
}catch (Exception e){
//如果找不到图片就返回一个默认图片
try {
response.sendRedirect("/static/img/tim.jpg");
} catch (IOException e1) {
e1.printStackTrace();
}
}finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java 下载文件
最新推荐文章于 2023-07-31 13:24:14 发布
4169

被折叠的 条评论
为什么被折叠?



