You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.9 KiB
188 lines
5.9 KiB
|
|
|
|
package com.heai.modules.ftp.controller;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import com.heai.common.exception.RRException;
|
|
import com.heai.common.utils.ConfigConstant;
|
|
import com.heai.common.utils.PageUtils;
|
|
import com.heai.common.utils.R;
|
|
import com.heai.common.utils.RandomUtil;
|
|
import com.heai.modules.ftp.util.FTPUtils;
|
|
import com.heai.modules.oss.entity.SysOssEntity;
|
|
import com.heai.modules.oss.service.SysOssService;
|
|
import com.heai.modules.sys.controller.AbstractController;
|
|
import com.heai.modules.sys.entity.SysUserEntity;
|
|
import com.heai.modules.sys.service.SysConfigService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.io.FileSystemResource;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.util.ClassUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Arrays;
|
|
import java.util.Date;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 文件上传
|
|
*
|
|
*
|
|
*/
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("ftp/file")
|
|
public class SysFtpController extends AbstractController {
|
|
@Autowired
|
|
private SysOssService sysOssService;
|
|
@Autowired
|
|
private SysConfigService sysConfigService;
|
|
|
|
private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;
|
|
|
|
/**
|
|
* 列表
|
|
*/
|
|
@GetMapping("/list")
|
|
public R list(@RequestParam Map<String, Object> params){
|
|
PageUtils page = sysOssService.queryPage(params);
|
|
|
|
return R.ok().put("page", page);
|
|
}
|
|
|
|
|
|
/**
|
|
* 上传文件
|
|
*/
|
|
@PostMapping("/upload")
|
|
public R upload(@RequestParam("file") MultipartFile file) throws Exception {
|
|
if (file.isEmpty()) {
|
|
throw new RRException("上传文件不能为空");
|
|
}
|
|
R r = new R();
|
|
//保存文件信息
|
|
SysOssEntity ossEntity = new SysOssEntity();
|
|
SysUserEntity user = getUser();
|
|
// 文件名
|
|
String fileName = file.getOriginalFilename();
|
|
// 文件类型
|
|
String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
|
|
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
// MultipartFile 转化 file
|
|
File newFile = FTPUtils.multipartFileToFile(file,fileName);
|
|
String newFileName = RandomUtil.getOrderNoByAtomic1(fileType)+suffix;
|
|
// 上传路径
|
|
String path = user.getUsername()+"/"+ DateUtil.format(new Date(),"yyyy-MM-dd");
|
|
// ftp上传 注意文件编码格式
|
|
boolean uploadStatus = FTPUtils.ftpUpload(path, newFileName, newFile);
|
|
ossEntity.setFileName(file.getOriginalFilename());
|
|
ossEntity.setNewFileName(newFileName);
|
|
ossEntity.setUrl(path);
|
|
ossEntity.setFileType(fileType);
|
|
ossEntity.setCreateDate(DateUtil.date());
|
|
ossEntity.setCreatedBy(user.getUsername());
|
|
ossEntity.setFileSuffix(suffix);
|
|
sysOssService.save(ossEntity);
|
|
newFile.delete();
|
|
return uploadStatus?R.ok().put("ossEntity", ossEntity):R.error("上附件失败");
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*/
|
|
@PostMapping("/delete")
|
|
@RequiresPermissions("sys:oss:all")
|
|
public R delete(@RequestBody Long[] ids){
|
|
sysOssService.removeByIds(Arrays.asList(ids));
|
|
return R.ok();
|
|
}
|
|
|
|
/**
|
|
* @Method multipartFileToFile
|
|
* @Description: MultipartFile 转 File
|
|
* @author zuowenwen
|
|
* @Version 1.0
|
|
* @param file
|
|
* @return java.io.File
|
|
* @throws
|
|
* @date 2020/12/17
|
|
*/
|
|
public static File multipartFileToFile(MultipartFile file ,String fileName) throws Exception {
|
|
File toFile = null;
|
|
if (file.equals("") || file.getSize() <= 0) {
|
|
file = null;
|
|
} else {
|
|
InputStream ins = null;
|
|
ins = file.getInputStream();
|
|
if(fileName != null && !"".equals(fileName)){
|
|
toFile = new File(fileName);
|
|
}else{
|
|
toFile = new File(file.getOriginalFilename());
|
|
}
|
|
inputStreamToFile(ins, toFile);
|
|
ins.close();
|
|
}
|
|
return toFile;
|
|
}
|
|
|
|
//获取流文件
|
|
private static void inputStreamToFile(InputStream ins, File file) {
|
|
try {
|
|
OutputStream os = new FileOutputStream(file);
|
|
int bytesRead = 0;
|
|
byte[] buffer = new byte[8192];
|
|
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
|
os.write(buffer, 0, bytesRead);
|
|
}
|
|
os.close();
|
|
ins.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Method downFtpFile
|
|
* @Description: ftp 文件流下载 (返回给前端一个文件流)
|
|
* @author sxm
|
|
* @Version 1.0
|
|
* @param id
|
|
* @return java.io.File
|
|
* @throws
|
|
* @date 2020/12/17
|
|
*/
|
|
@GetMapping("/downFtpFile/{id}")
|
|
public void downFtpFile(@PathVariable("id") Long id, HttpServletResponse response){
|
|
|
|
// SysOssEntity resultData =sysOssService.getById(id);
|
|
// String path = System.getProperty("user.dir");
|
|
// File file = new File(path+"/"+resultData.getFileName());
|
|
// FTPUtils.ftpDownload(resultData.getUrl(),resultData.getFileName(),file);
|
|
// HttpHeaders headers = new HttpHeaders();
|
|
// headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
// headers.add("Content-Disposition", "attachment; filename=" + file.getName());
|
|
// headers.add("Pragma", "no-cache");
|
|
// headers.add("Expires", "0");
|
|
// headers.add("Last-Modified", new Date().toString());
|
|
// headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
|
// return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
|
|
// //FTPUtils.downFile(response,"",resultData.getNewFileName(),resultData.getFileName());
|
|
|
|
SysOssEntity resultData =sysOssService.getById(id);
|
|
FTPUtils.downFile(response,resultData.getUrl(),resultData.getNewFileName(),resultData.getFileName());
|
|
}
|
|
|
|
}
|