package com.qxueyou.scc.sys.service.impl;
|
|
import java.io.BufferedInputStream;
|
import java.io.ByteArrayInputStream;
|
import java.io.File;
|
import java.io.FileInputStream;
|
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.boot.context.properties.EnableConfigurationProperties;
|
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.IOssService;
|
|
/**
|
*上传服务
|
*
|
* @author 德虎
|
*
|
*/
|
@Service
|
@EnableConfigurationProperties(SccConfig.class)
|
public class OssService extends CommonAppService implements IOssService {
|
|
/*
|
*
|
*/
|
// private OSSClient client;
|
|
private final Logger log = LogManager.getLogger("OssService");
|
|
/*
|
* 文档服务地址
|
*/
|
private String resDomain;
|
|
private static String resRootPath;
|
|
@Autowired
|
SccConfig sccConfig;
|
|
|
/**
|
* 初始化客户端
|
*/
|
@PostConstruct
|
private void init() {
|
resRootPath = sccConfig.getResRootPath();
|
}
|
|
/*
|
* (non-Javadoc)
|
*
|
* @see
|
* com.etraining.qxueyou.sys.service.IOssService#uplpadImage(java.io.File,
|
* java.lang.String)
|
*/
|
@Override
|
public Result uplpadImage(File file, String destPath) {
|
String fullPath = null;
|
try {
|
fullPath = resRootPath + destPath;
|
File fileRoot = new File(fullPath.substring(0, destPath.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);
|
}
|
|
@Override
|
public Result uplpadImage(InputStream ins,String fileFormat, String destPath) {
|
String fullPath = null;
|
try {
|
fullPath = resRootPath + destPath;
|
File fileRoot = new File(fullPath.substring(0, destPath.lastIndexOf("/")));
|
if(!fileRoot.exists()){
|
fileRoot.mkdirs();
|
}
|
FileUtils.copyInputStreamToFile(ins, new File(fullPath));
|
return new Result(true);
|
} catch (Exception e) {
|
log.error(e, e);
|
}finally{
|
IOUtils.closeQuietly(ins);
|
}
|
return new Result(false);
|
}
|
|
/*
|
* (non-Javadoc)
|
* @see com.etraining.qxueyou.sys.service.IOssService#uplpadVideo(java.io.InputStream, java.lang.String, long, java.lang.String)
|
*/
|
@Override
|
public Result uplpadFile(InputStream input,String fileFormat,long fileLength, String destPath) {
|
String fullPath = null;
|
try {
|
fullPath = resRootPath + destPath;
|
|
|
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);
|
}
|
|
/**
|
* 如果路径以 / 或 \ 开头,需要截取
|
* @param destPath
|
* @return
|
*/
|
public String chopPath(String destPath){
|
if(destPath.startsWith("/")||destPath.startsWith("\\")){
|
return destPath.substring(1);
|
}
|
return destPath;
|
}
|
|
/**
|
* 获取ContentType
|
* @param fileName
|
* @return
|
*/
|
public String getContentType(String fileName){
|
if(fileName.toUpperCase().endsWith("PNG")){
|
return "image/png";
|
}
|
return "image/jpeg";
|
}
|
|
@Override
|
public String getServerUrl() {
|
return resDomain;
|
}
|
|
@Override
|
public Result uplpadFile(File file, String destPath) {
|
String fullPath = null;
|
try {
|
fullPath = resRootPath + destPath;
|
File fileRoot = new File(fullPath.substring(0, destPath.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);
|
}
|
|
/*
|
* (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).replace("//", "/");
|
|
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;
|
}
|
|
/*
|
* (non-Javadoc)
|
* @see com.etraining.qxueyou.sys.service.IOssService#getObject(java.lang.String)
|
*/
|
@Override
|
public InputStream getObject(String key) {
|
File sourceFile = null;
|
InputStream in = null;
|
try {
|
sourceFile = new File(resRootPath + key);
|
if(sourceFile.exists()){
|
in = new FileInputStream(sourceFile);
|
}
|
} catch (Exception e) {
|
log.error(e, e);
|
}
|
return in;
|
}
|
|
@Override
|
public Result uploadFile(byte[] byteArray, String destPath) {
|
InputStream in = null;
|
|
String fullPath = null;
|
try {
|
fullPath = resRootPath + destPath;
|
File fileRoot = new File(fullPath.substring(0, destPath.lastIndexOf("/")));
|
if(!fileRoot.exists()){
|
fileRoot.mkdirs();
|
}
|
in= new BufferedInputStream(new ByteArrayInputStream(byteArray));
|
FileUtils.copyInputStreamToFile(in, new File(fullPath));
|
return new Result(true);
|
} catch (Exception e) {
|
log.error(e.getLocalizedMessage(), e);
|
return new Result(false);
|
}finally {
|
IOUtils.closeQuietly(in);
|
}
|
}
|
|
@Override
|
public void deleteOSSFile (String destPath) {
|
FileUtils.deleteQuietly(new File(resRootPath+destPath));
|
}
|
|
}
|