|
|
|
@ -3,8 +3,10 @@ package com.huoran.iasf.service.impl; |
|
|
|
|
import cn.hutool.core.io.FileUtil; |
|
|
|
|
import cn.hutool.core.io.IoUtil; |
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
import com.huoran.iasf.common.config.FileUploadProperties; |
|
|
|
|
import com.huoran.iasf.common.config.NonStaticResourceHttpRequestConfig; |
|
|
|
|
import com.huoran.iasf.common.exception.BusinessException; |
|
|
|
|
import com.huoran.iasf.common.exception.code.BaseResponseCode; |
|
|
|
|
import com.huoran.iasf.common.utils.DateUtils; |
|
|
|
@ -14,6 +16,7 @@ import com.huoran.iasf.service.SysFilesService; |
|
|
|
|
import com.huoran.iasf.vo.UEditorResultVO; |
|
|
|
|
import com.huoran.iasf.vo.req.FileParameters; |
|
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
@ -22,6 +25,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; |
|
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
|
import javax.servlet.ServletException; |
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
@ -29,6 +33,10 @@ import java.io.*; |
|
|
|
|
import java.net.InetAddress; |
|
|
|
|
import java.net.NetworkInterface; |
|
|
|
|
import java.net.URLEncoder; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
import java.nio.file.Paths; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.Enumeration; |
|
|
|
|
import java.util.List; |
|
|
|
@ -49,6 +57,9 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt |
|
|
|
|
@Resource |
|
|
|
|
private FileUploadProperties fileUploadProperties; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private NonStaticResourceHttpRequestConfig nonStaticResourceHttpRequestConfig; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public UEditorResultVO saveFile(MultipartFile file, FileParameters fileParameters) { |
|
|
|
|
if (ObjectUtil.isEmpty(file) || file.getSize() <= 0) { |
|
|
|
@ -180,26 +191,45 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void preview(String id, HttpServletRequest req, HttpServletResponse res) { |
|
|
|
|
public void preview(String id, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { |
|
|
|
|
if (id != null) { |
|
|
|
|
SysFilesEntity entity = this.getById(id); |
|
|
|
|
String path = entity.getFilePath(); |
|
|
|
|
res.setContentLength(entity.getFileSize()); |
|
|
|
|
|
|
|
|
|
// if (entity.getFormat().equals("mp4")){
|
|
|
|
|
//保存视频磁盘路径
|
|
|
|
|
Path filePath = Paths.get(path); |
|
|
|
|
|
|
|
|
|
//获取视频的类型,比如是MP4这样
|
|
|
|
|
File file = new File(path); |
|
|
|
|
byte[] buffer = FileUtil.readBytes(path); |
|
|
|
|
if (file.exists()) { |
|
|
|
|
try { |
|
|
|
|
//设置contentType
|
|
|
|
|
res.setContentType(MediaType.ALL_VALUE); |
|
|
|
|
res.setCharacterEncoding("UTF-8"); |
|
|
|
|
//获取outputStream
|
|
|
|
|
ServletOutputStream outputStream = res.getOutputStream(); |
|
|
|
|
//输出
|
|
|
|
|
IoUtil.write(outputStream, true, buffer); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
String mimeType = Files.probeContentType(filePath); |
|
|
|
|
if (StrUtil.isNotEmpty(mimeType)) { |
|
|
|
|
//判断类型,根据不同的类型文件来处理对应的数据
|
|
|
|
|
res.setContentType(mimeType); |
|
|
|
|
res.addHeader("Content-Length", "" + file.length()); |
|
|
|
|
} |
|
|
|
|
//转换视频流部分
|
|
|
|
|
req.setAttribute(NonStaticResourceHttpRequestConfig.ATTR_FILE, filePath); |
|
|
|
|
nonStaticResourceHttpRequestConfig.handleRequest(req, res); |
|
|
|
|
// }else {
|
|
|
|
|
// File file = new File(path);
|
|
|
|
|
// byte[] buffer = FileUtil.readBytes(path);
|
|
|
|
|
// if (file.exists()) {
|
|
|
|
|
// try {
|
|
|
|
|
// //设置contentType
|
|
|
|
|
// res.setContentType(MediaType.ALL_VALUE);
|
|
|
|
|
// res.setCharacterEncoding("UTF-8");
|
|
|
|
|
// //获取outputStream
|
|
|
|
|
// ServletOutputStream outputStream = res.getOutputStream();
|
|
|
|
|
// //输出
|
|
|
|
|
// IoUtil.write(outputStream, true, buffer);
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// log.error(e.getMessage(), e);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|