kkFileView对接svn服务完成文件在线预览功能

 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 }

本网页由快兔兔AI采集器生成,目的为演示采集效果,若侵权请及时联系删除。

原文链接:https://www.cnblogs.com/wang3680/p/16009777.html

更多内容