|
|
|
@ -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(), "上传失败,请选择文件!");
|
|
|
|
|