package com.qxueyou.scc.sys.service.impl; import java.io.File; import java.io.InputStream; import javax.annotation.PostConstruct; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.qxueyou.scc.base.model.Result; import com.qxueyou.scc.base.service.impl.CommonAppService; import com.qxueyou.scc.config.SccConfig; import com.qxueyou.scc.sys.service.IOssVideoService; /** *上传服务 * * @author 德虎 * */ @Service public class VideoOssService extends CommonAppService implements IOssVideoService { private final Logger log = LogManager.getLogger("VideoOssService"); @Autowired SccConfig sccConfig; private static String resRootPath; /** * 初始化客户端 */ @PostConstruct private void init() { resRootPath =sccConfig.getResRootPath(); } /** * 上传视频文件: */ public Result uplpadVideoFile(File file, String destPath) { String fullPath = null; try { fullPath = resRootPath + destPath; File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/"))); if(!fileRoot.exists()){ fileRoot.mkdirs(); } FileUtils.copyFile(file, new File(fullPath)); return new Result(true); } catch (Exception e) { log.error(e, e); } return new Result(false); } /** * 上传视频文件: */ public Result uplpadVideoFile(InputStream input,String fileFormat,long fileLength, String destPath) { String fullPath = null; try { fullPath = (resRootPath + destPath).replace("//", "/"); File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/"))); if(!fileRoot.exists()){ fileRoot.mkdirs(); } FileUtils.copyInputStreamToFile(input, new File(fullPath)); return new Result(true); } catch (Exception e) { log.error(e, e); }finally{ IOUtils.closeQuietly(input); } return new Result(false); } /* * (non-Javadoc) * @see com.etraining.qxueyou.sys.service.IOssService#getObject(java.lang.String, java.lang.String) */ @Override public File getObject(String key, String directory) { File sourceFile = null; File targetFile = null; String fullPath = null; try { sourceFile = new File(resRootPath + key); if(sourceFile.exists()){ fullPath = resRootPath + directory; File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/"))); if(!fileRoot.exists()){ fileRoot.mkdirs(); } targetFile = new File(resRootPath + directory); FileUtils.copyFile(sourceFile, targetFile); } } catch (Exception e) { log.error(e, e); } return targetFile; } /** * 如果路径以 / 或 \ 开头,需要截取 * @param destPath * @return */ public String chopPath(String destPath){ if(destPath.startsWith("/")||destPath.startsWith("\\")){ return destPath.substring(1); } return destPath; } @Override public boolean delObject(String key) { FileUtils.deleteQuietly(new File(resRootPath + key)); return true; } }