新增图片预览功能

segment2.0
linrf 2 years ago
parent 89a49c258d
commit 9d215a1340

@ -3,6 +3,7 @@ package com.docus.server.common.service;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@ -11,4 +12,6 @@ public interface IFileUploadService {
List<UploadFileVO> uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception;
void downloadFile(String filePath, HttpServletResponse response);
void getImage(String filePath, HttpServletResponse response, HttpServletRequest request) throws Exception;
}

@ -9,19 +9,26 @@ import com.docus.infrastructure.web.exception.ExceptionCode;
import com.docus.server.common.service.IFileUploadService;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@ -74,6 +81,7 @@ public class FileUploadServiceImpl implements IFileUploadService {
}
String fileName = path + multipartFile.getOriginalFilename();
File dest = new File(saveFilePath + fileName);
UploadFileVO uploadFileVO = new UploadFileVO();
@ -151,6 +159,82 @@ public class FileUploadServiceImpl implements IFileUploadService {
}
}
@Override
public void getImage(String filePath, HttpServletResponse response, HttpServletRequest request) throws Exception {
response.setContentType(ContentType.IMAGE_JPEG.toString());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
// 最终的图片
BufferedImage finalImage = null;
InputStream cacheFileInputStream = null;
try {
BufferedImage image = null;
// 文件的类型 pdf 1图片2
int pdfType = 1;
int imageType = 2;
// pdf 类型,将对应页码转为图片,默认 pdf全部在本地
image = readImageByStorageType(filePath);
// 得到的图片不为 null进行水印处理返回图片
if (null != image) {
// 压缩
finalImage = image;
image.flush();
Thumbnails.of(finalImage).
scale(1f).
outputFormat("jpg")
.toOutputStream(outputStream);
}
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
if (null != finalImage) {
finalImage.flush();
}
outputStream.flush();
outputStream.close();
if (cacheFileInputStream != null) {
cacheFileInputStream.close();
}
}
}
/**
*
*
* @param fileStorageType 3 1 2Ftp
* @param fileSrc
* @return
*/
private BufferedImage readImageByStorageType(String fileSrc) throws IOException {
BufferedImage image = null;
InputStream in = null;
ByteArrayInputStream byteArrayInputStream = null;
FileInputStream fileInputStream = null;
int local = 1;
int ftp = 2;
int smb = 3;
try {
fileInputStream = new FileInputStream(fileSrc);
in = new BufferedInputStream(fileInputStream);
if (in != null) {
image = ImageIO.read(in);
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
} finally {
if (null != in) {
in.close();
}
if (null != fileInputStream) {
fileInputStream.close();
}
}
return image;
}
private void valid(MultipartFile multipartFile) {
if (multipartFile.isEmpty()) {
throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "上传失败,请选择文件!");

@ -11,8 +11,6 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -24,9 +22,6 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;
/**
@ -54,6 +49,12 @@ public class FileController {
iFileUploadService.downloadFile(filePath, response);
}
@ApiOperation("获取图片")
@GetMapping("/getImage")
public void getImage(String filePath, HttpServletResponse response, HttpServletRequest request) throws Exception {
iFileUploadService.getImage(filePath, response, request);
}
@ApiOperation("文件上传")
@PostMapping("/upload")
@ApiImplicitParams({
@ -70,27 +71,6 @@ public class FileController {
return result;
}
/**
*
*/
@ApiOperation("获取图片")
@GetMapping(value = "/getImage")
public void getImage(String path, HttpServletRequest request, HttpServletResponse response) {
if (StringUtils.isNotBlank(path)) {
File image = new File(path);
if (image.exists()) {
response.setContentType("image/png");
try {
FileInputStream in = new FileInputStream(image);
OutputStream out = response.getOutputStream();
FileCopyUtils.copy(in, out);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
/**
*
*

Loading…
Cancel
Save