Commit e788c269 by 284718418@qq.com

文件存储地址配置切换

parent 6855c969
...@@ -13,6 +13,7 @@ import cn.timer.api.config.sftp.SftpConfiguration; ...@@ -13,6 +13,7 @@ import cn.timer.api.config.sftp.SftpConfiguration;
import cn.timer.api.controller.disk.sevice.DiskFilesService; import cn.timer.api.controller.disk.sevice.DiskFilesService;
import cn.timer.api.dto.disk.*; import cn.timer.api.dto.disk.*;
import cn.timer.api.service.FtpService; import cn.timer.api.service.FtpService;
import cn.timer.api.service.OSSService;
import cn.timer.api.utils.FileUtils; import cn.timer.api.utils.FileUtils;
import cn.timer.api.utils.Result; import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil; import cn.timer.api.utils.ResultUtil;
...@@ -62,6 +63,8 @@ public class DiskFilesController { ...@@ -62,6 +63,8 @@ public class DiskFilesController {
@Autowired @Autowired
private SftpConfiguration config; private SftpConfiguration config;
@Autowired
private OSSService ossService;
@ResponseBody @ResponseBody
@PostMapping("/upload") @PostMapping("/upload")
...@@ -103,12 +106,12 @@ public class DiskFilesController { ...@@ -103,12 +106,12 @@ public class DiskFilesController {
List<FileInfoDto> imageUrls; List<FileInfoDto> imageUrls;
try { try {
//上传文件到服务器 //上传文件到服务器
imageUrls = ftpService.uploadFile(file); imageUrls = ftpService.uploadFile(userBean.getOrgCode(),file);
for (FileInfoDto dto : imageUrls) { for (FileInfoDto dto : imageUrls) {
//新增资源上传文件 //新增资源上传文件
diskFiles.setFileType(dto.getFileSuffix()); diskFiles.setFileType(dto.getFileSuffix());
//"/home/disk/123456.jpg" //"/home/disk/123456.jpg"
diskFiles.setDiskPath(config.getRoot() + config.getTargetPath() + "/" + dto.getFileName()); diskFiles.setDiskPath(dto.getFileName());
diskFiles.setUrlPath(dto.getUrlPath()); diskFiles.setUrlPath(dto.getUrlPath());
diskFiles.setTitle(dto.getResourceFileName()); diskFiles.setTitle(dto.getResourceFileName());
diskFiles.setFileSize(dto.getFileSize()); diskFiles.setFileSize(dto.getFileSize());
...@@ -260,7 +263,7 @@ public class DiskFilesController { ...@@ -260,7 +263,7 @@ public class DiskFilesController {
// filePath = new File(diskFiles.getDiskPath()); // filePath = new File(diskFiles.getDiskPath());
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
// fis = new FileInputStream(filePath); // fis = new FileInputStream(filePath);
fis = ftpService.downloadFile(diskFiles.getDiskPath()); fis = ftpService.downloadFile(diskFiles);
bis = new BufferedInputStream(fis); bis = new BufferedInputStream(fis);
os = resp.getOutputStream(); os = resp.getOutputStream();
int i = bis.read(buffer); int i = bis.read(buffer);
......
package cn.timer.api.service; package cn.timer.api.service;
import cn.timer.api.bean.disk.DiskFiles;
import cn.timer.api.dto.disk.FileInfoDto; import cn.timer.api.dto.disk.FileInfoDto;
import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -21,14 +22,14 @@ public interface FtpService { ...@@ -21,14 +22,14 @@ public interface FtpService {
* @param files * @param files
* @return * @return
*/ */
List<FileInfoDto> uploadFile(MultipartFile[] files); List<FileInfoDto> uploadFile(int orgCode, MultipartFile[] files);
/** /**
* 下载单个文件 * 下载单个文件
* *
* @param remotePath:远程目录 * @param remotePath:远程目录
* @return InputStream * @return InputStream
*/ */
InputStream downloadFile(String remotePath); InputStream downloadFile(DiskFiles diskFiles);
/** /**
* 删除服务器文件 * 删除服务器文件
......
package cn.timer.api.service; package cn.timer.api.service;
import cn.timer.api.dto.disk.FileInfoDto;
import cn.timer.api.service.impl.OSSServiceImpl; import cn.timer.api.service.impl.OSSServiceImpl;
import cn.timer.api.utils.Result; import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil; import cn.timer.api.utils.ResultUtil;
...@@ -11,6 +12,7 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -11,6 +12,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
...@@ -86,4 +88,43 @@ public class OSSService implements OSSServiceImpl { ...@@ -86,4 +88,43 @@ public class OSSService implements OSSServiceImpl {
} }
return ResultUtil.data(list, "上传成功!"); return ResultUtil.data(list, "上传成功!");
} }
@Override
public List<FileInfoDto> uploadFile(int orgCode, String moudle,MultipartFile[] files) {
List<FileInfoDto> resultStrs = new ArrayList<FileInfoDto>();
FileInfoDto dto = null;
for (MultipartFile file : files) {
String randomNickname = RandomNum.getRandomNickname();
String name = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
File filePath = filePathUpload(orgCode, moudle, randomNickname, file.getOriginalFilename());
int i = 0;
while (filePath.exists()) {
filePath = filePathUpload(orgCode,moudle,randomNickname,file.getOriginalFilename());
i++;
}
if (file == null || file.getSize() <= 0) {
// return ResultUtil.error("上传的文件为空,请重新选择!");
} else {
try {
dto = new FileInfoDto();
dto.setUrlPath(oss.uploadFile(filePath, file.getInputStream()));
dto.setResourceFileName(name);
dto.setFileName(filePath.getPath());
/*Float size = Float.parseFloat(String.valueOf(file.getSize())) / 1024;
BigDecimal b = new BigDecimal(size);
// 2表示2位 ROUND_HALF_UP表明四舍五入,
size = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();*/
dto.setFileSize(file.getSize());
dto.setFileSuffix(suffix);
resultStrs.add(dto);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resultStrs;
}
} }
package cn.timer.api.service.impl; package cn.timer.api.service.impl;
import cn.timer.api.bean.disk.DiskFiles;
import cn.timer.api.config.sftp.SftpConfiguration; import cn.timer.api.config.sftp.SftpConfiguration;
import cn.timer.api.controller.disk.constant.FileAddressConstant; import cn.timer.api.controller.disk.constant.FileAddressConstant;
import cn.timer.api.dto.disk.FileInfoDto; import cn.timer.api.dto.disk.FileInfoDto;
import cn.timer.api.service.FtpService; import cn.timer.api.service.FtpService;
import cn.timer.api.service.OSSService;
import cn.timer.api.utils.Md5; import cn.timer.api.utils.Md5;
import cn.timer.api.utils.aliyun.OSSUtil; import cn.timer.api.utils.aliyun.OSSUtil;
import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp;
...@@ -30,6 +32,8 @@ public class FtpServiceImpl implements FtpService { ...@@ -30,6 +32,8 @@ public class FtpServiceImpl implements FtpService {
private SftpConfiguration config; private SftpConfiguration config;
@Autowired @Autowired
private OSSUtil oss; private OSSUtil oss;
@Autowired
private OSSService ossService;
@Override @Override
...@@ -66,7 +70,7 @@ public class FtpServiceImpl implements FtpService { ...@@ -66,7 +70,7 @@ public class FtpServiceImpl implements FtpService {
} }
@Override @Override
public List<FileInfoDto> uploadFile(MultipartFile[] files) { public List<FileInfoDto> uploadFile(int orgCode, MultipartFile[] files) {
List<FileInfoDto> resultStrs = new ArrayList<FileInfoDto>(); List<FileInfoDto> resultStrs = new ArrayList<FileInfoDto>();
//存储地址判断 //存储地址判断
...@@ -94,7 +98,7 @@ public class FtpServiceImpl implements FtpService { ...@@ -94,7 +98,7 @@ public class FtpServiceImpl implements FtpService {
sftp.put(file.getInputStream(), fileName); sftp.put(file.getInputStream(), fileName);
dto.setUrlPath(config.getServerUrl() + "/" + fileName); dto.setUrlPath(config.getServerUrl() + "/" + fileName);
dto.setResourceFileName(name); dto.setResourceFileName(name);
dto.setFileName(fileName); dto.setFileName(config.getRoot() + config.getTargetPath() + "/" +fileName);
/*Float size = Float.parseFloat(String.valueOf(file.getSize())) / 1024; /*Float size = Float.parseFloat(String.valueOf(file.getSize())) / 1024;
BigDecimal b = new BigDecimal(size); BigDecimal b = new BigDecimal(size);
...@@ -113,7 +117,7 @@ public class FtpServiceImpl implements FtpService { ...@@ -113,7 +117,7 @@ public class FtpServiceImpl implements FtpService {
config.returnSftpSocket(sftp); config.returnSftpSocket(sftp);
} }
} else if (FileAddressConstant.ALIYUN_OSS.equals(config.getFileAddress())) { } else if (FileAddressConstant.ALIYUN_OSS.equals(config.getFileAddress())) {
resultStrs = ossService.uploadFile(orgCode, "disk",files);
} }
return resultStrs; return resultStrs;
} }
...@@ -122,28 +126,33 @@ public class FtpServiceImpl implements FtpService { ...@@ -122,28 +126,33 @@ public class FtpServiceImpl implements FtpService {
/** /**
* 下载单个文件 * 下载单个文件
* *
* @param remotePath:远程下载目录 * @param diskFiles:远程下载目录
* @return * @return
*/ */
@Override @Override
public InputStream downloadFile(String remotePath) { public InputStream downloadFile(DiskFiles diskFiles) {
ChannelSftp sftp = config.getSftpSocket(); //存储地址判断
InputStream nputStream = null; InputStream nputStream = null;
try { if (diskFiles.getUrlPath().indexOf(config.getServerUrl())!=-1) {
// sftp.cd(remotePath); ChannelSftp sftp = config.getSftpSocket();
// mkdirs(localPath + localFileName); try {
//sftp.get(remotePath, fieloutput); // sftp.cd(remotePath);
nputStream = sftp.get(remotePath); // mkdirs(localPath + localFileName);
if (log.isInfoEnabled()) { //sftp.get(remotePath, fieloutput);
log.info("===DownloadFile:" + remotePath + " success from sftp."); nputStream = sftp.get(diskFiles.getDiskPath());
if (log.isInfoEnabled()) {
log.info("===DownloadFile:" + diskFiles.getDiskPath() + " success from sftp.");
}
return nputStream;
} catch (SftpException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
config.returnSftpSocket(sftp);
} }
return nputStream; }else if (FileAddressConstant.ALIYUN_OSS.equals(config.getFileAddress())) {
} catch (SftpException e) { nputStream = oss.downloadFileInputStream(diskFiles.getDiskPath());
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
config.returnSftpSocket(sftp);
} }
return nputStream; return nputStream;
} }
......
package cn.timer.api.service.impl; package cn.timer.api.service.impl;
import cn.timer.api.dto.disk.FileInfoDto;
import cn.timer.api.utils.Result; import cn.timer.api.utils.Result;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.List; import java.util.List;
public interface OSSServiceImpl { public interface OSSServiceImpl {
public Result<String> upload(int orgCode, String moudle, MultipartFile file); public Result<String> upload(int orgCode, String moudle, MultipartFile file);
public Result<Object> uploads(int orgCode, String moudle, List<MultipartFile> files); public Result<Object> uploads(int orgCode, String moudle, List<MultipartFile> files);
/**
* 上传文件到阿里云服务器
* @param files
* @return
*/
List<FileInfoDto> uploadFile(int orgCode, String moudle,MultipartFile[] files);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment