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