本文共 5237 字,大约阅读时间需要 17 分钟。
JAVA视频MP4文件加密,Html5播放器调用
关键词:视频加密,mp4加密
package com.xgt.util;import java.io.*;public class VideoEncodeUtil { public static void main(String[] args) throws Exception { encrypt("D:\\py交易\\11.mp4","fuckyourself"); decrypt("D:\\py交易\\11.mp4","D:\\py交易\\22.mp4",4); System.out.println(readFileLastByte("D:\\py交易\\11.mp4",12)); } /** * 文件file进行加密 * @param fileUrl 文件路径 * @param key 密码 * @throws Exception */ public static void encrypt(String fileUrl, String key) throws Exception { File file = new File(fileUrl); String path = file.getPath(); if(!file.exists()){ return; } int index = path.lastIndexOf("\\"); String destFile = path.substring(0, index)+"\\"+"abc"; File dest = new File(destFile); //获取待加密文件的输入流 InputStream in = new FileInputStream(fileUrl); //创建中转文件输出流 OutputStream out = new FileOutputStream(destFile); //待加密文件的流 byte[] buffer = new byte[1024]; int r; //加密之后的文件的流 byte[] buffer2=new byte[1024]; while (( r= in.read(buffer)) > 0) { for(int i=0;i>10); int count = mod==0?div:(div+1); int k = 1, r; while ((k <= count && ( r = is.read(buffer)) > 0)) { if(mod != 0 && k==count) { r = mod; } for(int i = 0;i < r;i++) { byte b=buffer[i]; //buffer2[i]=b==0?bMax:--b; //每个字节减2解码 b-=2; buffer2[i] = b; } out.write(buffer2, 0, r); k++; } out.close(); is.close(); return tempUrl; } /** * 判断文件是否加密 * @param fileName * @return * * 加密成功返回key * 加密失败返回非key的字符串 */ public static String readFileLastByte(String fileName, int keyLength) { File file = new File(fileName); if(!file.exists())return "没有文件"; StringBuffer str = new StringBuffer(); try { // 打开一个随机访问文件流,按读写方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 文件长度,字节数 long fileLength = randomFile.length(); //将写文件指针移到文件尾。 for(int i = keyLength ; i>=1 ; i--){ randomFile.seek(fileLength-i); str.append((char)randomFile.read()); } randomFile.close(); return str.toString(); } catch (IOException e) { e.printStackTrace(); } return "异常"; }}
/** * 加密视频 * @param fileName */ public void encryptVideo(String fileName){ try { File file = new File(fileUrl); FileToolUtil.judeDirExists(file); /** * 加密不可进行多次 * 判断是否有加密,加密过无需在加密 */ if(!VideoEncodeUtil.readFileLastByte(fileUrl+"\\"+fileName,key.length()).equals(key)) { VideoEncodeUtil.encrypt(fileUrl + "\\" + fileName, key); } } catch (Exception e) { e.printStackTrace(); } } /** * 解密视频 * @param fileName */ public String decryptVideo(String fileName) throws Exception { File file = new File(SystemConstant.VIDEO_TEMP_PATH); FileToolUtil.judeDirExists(file); return VideoEncodeUtil.decrypt(SAFETY_EDU_LOCAL_PATH + fileName, SystemConstant.VIDEO_TEMP_PATH + fileName, SystemConstant.VIDEO_ENCRYPT_DECRYPT_KEY.length()); }
/** * 播放加密MP4 * @param response * @throws IOException */ @RequestMapping("/playMp4") @ResponseBody public void playMp4(HttpServletResponse response,String fileName) throws Exception { // TODO 测试联调结束后需删除 fileName = "edu/video/6689AC1A506133C13FDA593135E2D923.mp4"; String tempFilePath = SystemConstant.VIDEO_TEMP_PATH + fileName; try { tempFilePath = videoMaterialService.decryptVideo(fileName); // 解密过后的临时文件路径 FileInputStream inputStream = new FileInputStream(tempFilePath); byte[] data = FileToolUtil.inputStreamToByte(inputStream); String diskfilename = "final.mp4"; response.setContentType("video/mp4"); response.setHeader("Content-Disposition", "attachment; filename=\"" + diskfilename + "\"" ); System.out.println("data.length " + data.length); response.setContentLength(data.length); response.setHeader("Content-Range", "" + Integer.valueOf(data.length-1)); response.setHeader("Accept-Ranges", "bytes"); response.setHeader("Etag", "W/\"9767057-1323779115364\""); OutputStream os = response.getOutputStream(); os.write(data); //先声明的流后关掉! os.flush(); os.close(); inputStream.close(); }catch (Exception e){ logger.error("....", e); }finally { VideoMaterialService.deleteVideoFile(tempFilePath); } }
来源Rest探路者 cnblogs.com/Java-Starter/p/9300292.html
转载地址:http://exztd.baihongyu.com/