1.需求:
之前在公司内部搭建了svn伺服器,給部門存放文檔、視訊,做成了一個文檔伺服器來用,随着視訊檔案太大,每次下載下傳太慢
需要把檔案線上打開檢視
2.解決:
kkFileView
https://kkfileview.keking.cn/zh-cn/index.html
kkFileView為檔案文檔線上預覽解決方案,該項目使用流行的spring boot搭建,易上手和部署,基本支援主流辦公文檔的線上預覽,如doc,docx,xls,xlsx,ppt,pptx,pdf,txt,zip,rar,圖檔,視訊,音頻等等
我之前寫過的svn部署文檔
kkFileView無法适配svn服務,需要開發svn用戶端完成svn伺服器檔案的checkout操作
3.改造kkFileView适配svn服務
我用的版本是4.0.0 GitHub位址 https://github.com/kekingcn/kkFileView
下載下傳源碼,導入到idea中
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.9.3</version>
</dependency>
- 新增SVNUtils.java 在 package cn.keking.utils;
1 package cn.keking.utils;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.tmatesoft.svn.core.*;
6 import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
7 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
8 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
9 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
10 import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
11 import org.tmatesoft.svn.core.io.SVNRepository;
12 import org.tmatesoft.svn.core.wc.*;
13
14 import java.io.File;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.OutputStream;
18
19 /**
20 * @program: filepreview
21 * @ClassName SVNUtils
22 * @description: svn下的https檔案下載下傳
23 * @author: wangshaoshuai
24 * @create: 2022/3/14 16:34
25 */
26 public class SVNUtils {
27 private final static Logger logger = LoggerFactory.getLogger(SVNUtils.class);
28
29 private String url;
30 private String username;
31 private String password;
32 private SVNRepository repository;
33
34 public SVNUtils(String url, String username, String password) {
35 super();
36 this.url = url;
37 this.username = username;
38 this.password = password;
39 initialize();
40 }
41
42 /**
43 * 初始化操作
44 *
45 * @throws SVNException
46 */
47 private void initialize() {
48 FSRepositoryFactory.setup();
49 DAVRepositoryFactory.setup();
50 SVNRepositoryFactoryImpl.setup();
51 try {
52 repository = SVNRepositoryFactoryImpl.create(SVNURL.parseURIEncoded(this.url));
53 ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(this.username, this.password);
54 repository.setAuthenticationManager(authManager);
55 } catch (SVNException e) {
56 e.printStackTrace();
57 }
58 }
59
60 /**
61 * 從SVN伺服器擷取最新版本的檔案
62 *
63 * @param filePath 相對于倉庫根目錄的路徑
64 * @return 傳回checkout檔案的版本号
65 * @throws Exception 可以自定義Exception
66 */
67 public long getFileFromSVN(String filePath, String outFileName) {
68 return getFileFromSVN(filePath, outFileName, 0);
69 }
70
71 /**
72 * 從SVN伺服器擷取檔案
73 *
74 * @param filePath 相對于倉庫根目錄的路徑
75 * @param version 要checkout的版本号,當傳入的版本号為0時,預設擷取最新版本
76 * @return 傳回checkout檔案的版本号
77 * @throws Exception 可以自定義Exception
78 */
79 public long getFileFromSVN(String filePath, String outFileName, long version) {
80 SVNNodeKind node = null;
81 try {
82 if (version == 0) {
83 version = repository.getLatestRevision();
84 }
85 // node = repository.checkPath(filePath, version);
86 } catch (SVNException e) {
87 logger.error("SVN檢測不到該檔案:" + filePath, e);
88 }
89 // if (node != SVNNodeKind.FILE) {
90 // logger.error(node.toString() + "不是檔案");
91 // }
92 SVNProperties properties = new SVNProperties();
93 try {
94 OutputStream outputStream = new FileOutputStream(outFileName);
95 repository.getFile(filePath, version, properties, outputStream);
96 outputStream.close();
97 } catch (SVNException e) {
98 logger.error("擷取SVN伺服器中的" + filePath + "檔案失敗", e);
99 } catch (IOException e) {
100 logger.error("SVN check out file faild.", e);
101 }
102 return Long.parseLong(properties.getStringValue("svn:entry:revision"));
103 }
104
- View Code
- 修改DownloadUtils.java 具體位置為package cn.keking.utils;
1 package cn.keking.utils;
2
3 import cn.keking.config.ConfigConstants;
4 import cn.keking.model.FileAttribute;
5 import cn.keking.model.ReturnResponse;
6 import io.mola.galimatias.GalimatiasParseException;
7 import org.apache.commons.io.FileUtils;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import java.io.*;
12 import java.net.*;
13 import java.util.UUID;
14
15 import static cn.keking.utils.KkFileUtils.isFtpUrl;
16 import static cn.keking.utils.KkFileUtils.isHttpUrl;
17
18 /**
19 * @author yudian-it
20 */
21 public class DownloadUtils {
22
23 private final static Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
24 private static final String fileDir = ConfigConstants.getFileDir();
25 private static final String URL_PARAM_FTP_USERNAME = "ftp.username";
26 private static final String URL_PARAM_FTP_PASSWORD = "ftp.password";
27 private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding";
28
29 private static final String URL_PARAM_URL_TYPE = "url.type";
30 private static final String URL_PARAM_SVN_USERNAME = "svn.username";
31 private static final String URL_PARAM_SVN_PASSWORD = "svn.password";
32
33 /**
34 * @param fileAttribute fileAttribute
35 * @param fileName 檔案名
36 * @return 本地檔案絕對路徑
37 */
38 public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
39 String urlStr = fileAttribute.getUrl();
40 ReturnResponse<String> response = new ReturnResponse<>(0, "下載下傳成功!!!", "");
41 String realPath = DownloadUtils.getRelFilePath(fileName, fileAttribute);
42 try {
43 URL url = WebUtils.normalizedURL(urlStr);
44 if (isHttpUrl(url)) {
45 File realFile = new File(realPath);
46 String url_type = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_URL_TYPE);
47 if(url_type.equalsIgnoreCase("svn")){//svn的服務的話下載下傳方式不一樣
48 String svn_username = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_USERNAME);
49 String svn_password = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_PASSWORD);
50 String svn_folder = urlStr.substring(0,urlStr.lastIndexOf("/"));
51 String file_name = urlStr.substring(urlStr.lastIndexOf("/")+1,urlStr.indexOf("?"));
52 SVNUtils svnUtils = new SVNUtils(svn_folder,svn_username,svn_password);
53 svnUtils.getFileFromSVN(file_name,realPath);
54 }else{
55 FileUtils.copyURLToFile(url,realFile);
56 }
57 } else if (isFtpUrl(url)) {
58 String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
59 String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
60 String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
61 FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
62 } else {
63 response.setCode(1);
64 response.setMsg("url不能識别url" + urlStr);
65 }
66 response.setContent(realPath);
67 response.setMsg(fileName);
68 return response;
69 } catch (IOException | GalimatiasParseException e) {
70 logger.error("檔案下載下傳失敗,url:{}", urlStr, e);
71 response.setCode(1);
72 response.setContent(null);
73 if (e instanceof FileNotFoundException) {
74 response.setMsg("檔案不存在!!!");
75 } else {
76 response.setMsg(e.getMessage());
77 }
78 return response;
79 }
80 }
81
82
83 /**
84 * 擷取真實檔案絕對路徑
85 *
86 * @param fileName 檔案名
87 * @return 檔案路徑
88 */
89 private static String getRelFilePath(String fileName, FileAttribute fileAttribute) {
90 String type = fileAttribute.getSuffix();
91 if (null == fileName) {
92 UUID uuid = UUID.randomUUID();
93 fileName = uuid + "." + type;
94 } else { // 檔案字尾不一緻時,以type為準(針對simText【将類txt檔案轉為txt】)
95 fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
96 }
97 String realPath = fileDir + fileName;
98 File dirFile = new File(fileDir);
99 if (!dirFile.exists() && !dirFile.mkdirs()) {
100 logger.error("建立目錄【{}】失敗,可能是權限不夠,請檢查", fileDir);
101 }
102 return realPath;
103 }
104
105
- View Code
1 public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
2 String urlStr = fileAttribute.getUrl();
3 ReturnResponse<String> response = new ReturnResponse<>(0, "下載下傳成功!!!", "");
4 String realPath = DownloadUtils.getRelFilePath(fileName, fileAttribute);
5 try {
6 URL url = WebUtils.normalizedURL(urlStr);
7 if (isHttpUrl(url)) {
8 File realFile = new File(realPath);
9 String url_type = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_URL_TYPE);
10 if(url_type.equalsIgnoreCase("svn")){//svn的服務的話下載下傳方式不一樣
11 String svn_username = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_USERNAME);
12 String svn_password = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_SVN_PASSWORD);
13 String svn_folder = urlStr.substring(0,urlStr.lastIndexOf("/"));
14 String file_name = urlStr.substring(urlStr.lastIndexOf("/")+1,urlStr.indexOf("?"));
15 SVNUtils svnUtils = new SVNUtils(svn_folder,svn_username,svn_password);
16 svnUtils.getFileFromSVN(file_name,realPath);
17 }else{
18 FileUtils.copyURLToFile(url,realFile);
19 }
20 } else if (isFtpUrl(url)) {
21 String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
22 String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
23 String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
24 FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
25 } else {
26 response.setCode(1);
27 response.setMsg("url不能識别url" + urlStr);
28 }
29 response.setContent(realPath);
30 response.setMsg(fileName);
31 return response;
32 } catch (IOException | GalimatiasParseException e) {
33 logger.error("檔案下載下傳失敗,url:{}", urlStr, e);
34 response.setCode(1);
35 response.setContent(null);
36 if (e instanceof FileNotFoundException) {
37 response.setMsg("檔案不存在!!!");
38 } else {
39 response.setMsg(e.getMessage());
40 }
41 return response;
42 }
43
主要是修改這段代碼,增加svn類型的判斷和工具類的使用
- 使用方法如下:a.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>kkFileView示範首頁</title>
</head>
<body>
<div>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/base64.min.js"></script>
<script>
var url = 'https://127.0.0.1/svn/業務學習資料/資料整理文檔/JAVA學習/JAVA開發工具idea的使用01-王少帥.mp4';
var pre_url = url + '?url.type=svn&svn.username=wangshaoshuai&svn.password=123456';
window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(pre_url)));
</script>
</body>
</html>
1 var url = 'https://127.0.0.1/svn/業務學習資料/資料整理文檔/JAVA學習/JAVA開發工具idea的使用01-王少帥.mp4';
2 var pre_url = url + '?url.type=svn&svn.username=wangshaoshuai&svn.password=123456';
3
主要是這段代碼,即可加入到svn清單顯示的服務裡面會彈窗跳轉一個新的預覽頁面
4.測試驗證
- idea下面啟動服務
- 用浏覽器打開a.html
點選播放即可播放
kkFileView部署到windows服務出現問題解決
作者:少帥