文件下载 查看视频密码
第一次登录送5个金币,绑定QQ送5个金币。金币可以用来观看视频和下载资源哦!
ssm的
@RequestMapping(value = "/downfile", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") @ResponseBody public String downfile(HttpSession session,HttpServletResponse response,MuMedia o,HttpServletRequest request) throws Exception { o=mediaService.queryById(o.getId()); String f=""; if(o.getType().equals("audio"))f=o.getAudio_src(); if(o.getType().equals("video"))f=o.getVideo_src(); String format=f.substring(f.indexOf("."), f.length()); String abPath= Sys.Upimg.absolute_path+f; abPath=abPath.replace("/", "\\"); String filepath = abPath; // 如果文件名不为空,则进行下载 File file = new File(filepath); // 如果文件存在,则进行下载 if (file.exists()) { // 配置文件下载 response.setHeader("content-type", "application/octet-stream"); response.setContentType("application/octet-stream"); // 下载文件能正常显示中文 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(o.getTitle()+format, "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!"); return "1"; } catch (Exception e) { return "0"; } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }else{ return "-1"; } }
springboot的
@RequestMapping(value = "/downfile", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public void downfile(HttpSession session,HttpServletResponse response,Uploadfiles o,HttpServletRequest request) throws Exception { o=uploadfilesService.selectById(o.getId()); String abPath= request.getSession().getServletContext().getRealPath(o.getUrl()) ; abPath=abPath.replace("/", "\\"); String filepath = abPath; try { // path是指想要下载的文件的路径 File file = new File(filepath); // 将文件写入输入流 FileInputStream fileInputStream = new FileInputStream(file); InputStream fis = new BufferedInputStream(fileInputStream); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header response.setCharacterEncoding("UTF-8"); //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存 //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3" // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(o.getFname(), "UTF-8")); // 告知浏览器文件的大小 response.addHeader("Content-Length", "" + file.length()); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); outputStream.write(buffer); outputStream.flush(); } catch (IOException ex) { ex.printStackTrace(); } }