|
@@ -1,9 +1,13 @@
|
|
|
package com.ruoyi.web.controller.common;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
+import com.ruoyi.common.config.RuoYiConfig;
|
|
|
+import com.ruoyi.common.constant.Constants;
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import com.ruoyi.common.utils.file.FileUploadUtils;
|
|
|
+import com.ruoyi.common.utils.file.FileUtils;
|
|
|
import com.ruoyi.common.utils.file.MimeTypeUtils;
|
|
|
+import com.ruoyi.framework.config.ServerConfig;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -12,13 +16,16 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
-import com.ruoyi.common.config.RuoYiConfig;
|
|
|
-import com.ruoyi.common.constant.Constants;
|
|
|
-import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
-import com.ruoyi.common.utils.StringUtils;
|
|
|
-import com.ruoyi.common.utils.file.FileUploadUtils;
|
|
|
-import com.ruoyi.common.utils.file.FileUtils;
|
|
|
-import com.ruoyi.framework.config.ServerConfig;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.geom.Rectangle2D;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
|
|
|
import static com.ruoyi.common.utils.file.FileUploadUtils.getExtension;
|
|
|
import static com.ruoyi.common.utils.file.FileUploadUtils.isPicture;
|
|
@@ -30,8 +37,7 @@ import static com.ruoyi.common.utils.file.ImageUtils.imageMethod;
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
@RestController
|
|
|
-public class CommonController
|
|
|
-{
|
|
|
+public class CommonController {
|
|
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
|
|
|
|
@Autowired
|
|
@@ -41,13 +47,11 @@ public class CommonController
|
|
|
* 通用下载请求
|
|
|
*
|
|
|
* @param fileName 文件名称
|
|
|
- * @param delete 是否删除
|
|
|
+ * @param delete 是否删除
|
|
|
*/
|
|
|
@GetMapping("common/download")
|
|
|
- public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
/*if (!FileUtils.checkAllowDownload(fileName))
|
|
|
{
|
|
|
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
|
|
@@ -59,13 +63,10 @@ public class CommonController
|
|
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
FileUtils.setAttachmentResponseHeader(response, realFileName);
|
|
|
FileUtils.writeBytes(filePath, response.getOutputStream());
|
|
|
- if (delete)
|
|
|
- {
|
|
|
+ if (delete) {
|
|
|
FileUtils.deleteFile(filePath);
|
|
|
}
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("下载文件失败", e);
|
|
|
}
|
|
|
}
|
|
@@ -74,10 +75,8 @@ public class CommonController
|
|
|
* 通用上传请求
|
|
|
*/
|
|
|
@PostMapping("/common/upload")
|
|
|
- public AjaxResult uploadFile(MultipartFile file) throws Exception
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
|
|
+ try {
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
// 上传文件路径
|
|
|
String filePath = RuoYiConfig.getUploadPath();
|
|
@@ -91,18 +90,18 @@ public class CommonController
|
|
|
}
|
|
|
// 上传并返回新文件名称
|
|
|
String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
- if (serverConfig.getUrl().contains("qd.dmu.com.cn")){
|
|
|
- String url = serverConfig.getUrl()+":8443" + fileName;
|
|
|
- if (url.indexOf("http")!= -1){
|
|
|
+ if (serverConfig.getUrl().contains("qd.dmu.com.cn")) {
|
|
|
+ String url = serverConfig.getUrl() + ":8443" + fileName;
|
|
|
+ if (url.indexOf("http") != -1) {
|
|
|
String replace = url.replace("http", "https");
|
|
|
url = replace;
|
|
|
}
|
|
|
ajax.put("fileName", file.getOriginalFilename());
|
|
|
ajax.put("url", url);
|
|
|
return ajax;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
String url = serverConfig.getUrl() + fileName;
|
|
|
- if (url.indexOf("http")!= -1){
|
|
|
+ if (url.indexOf("http") != -1) {
|
|
|
String replace = url.replace("http", "https");
|
|
|
url = replace;
|
|
|
}
|
|
@@ -110,9 +109,76 @@ public class CommonController
|
|
|
ajax.put("url", url);
|
|
|
return ajax;
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ return AjaxResult.error(e.getMessage());
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用上传请求
|
|
|
+ */
|
|
|
+ @PostMapping("/common/upload/watermark")
|
|
|
+ public AjaxResult uploadFileWatermark(MultipartFile file) throws Exception {
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ return AjaxResult.error("上传失败请重试1");
|
|
|
+ }
|
|
|
+ Date date = new Date();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String formattedDate = sdf.format(date);
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ BufferedImage originalImage = ImageIO.read(inputStream);
|
|
|
+ int widthSize = (int) (originalImage.getWidth() * 0.5);
|
|
|
+ int heightSize = (int) (originalImage.getHeight() * 0.5);
|
|
|
+ Image compressedImage = originalImage.getScaledInstance(widthSize, heightSize, Image.SCALE_SMOOTH);
|
|
|
+ BufferedImage outputImage = new BufferedImage(widthSize, heightSize, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D g2d = (Graphics2D) outputImage.getGraphics();
|
|
|
+ g2d.drawImage(compressedImage, 0, 0, null);
|
|
|
+ OutputStream outputStream = new FileOutputStream(new File(file.getName()+getExtension(file)));
|
|
|
+ AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
|
|
|
+ g2d.setComposite(alphaChannel);
|
|
|
+ g2d.setColor(Color.BLUE);
|
|
|
+ g2d.setFont(new Font("Arial", Font.BOLD, 30));
|
|
|
+ // 获取图像的宽度和高度
|
|
|
+ int width = outputImage.getWidth();
|
|
|
+ int height = outputImage.getHeight();
|
|
|
+ // 计算水印位置为右下角
|
|
|
+ Rectangle2D rect = g2d.getFont().getStringBounds(formattedDate, g2d.getFontRenderContext());
|
|
|
+ float textWidth = (float) rect.getWidth();
|
|
|
+ float textHeight = (float) rect.getHeight();
|
|
|
+ float x = width - textWidth;
|
|
|
+ float y = height - textHeight;
|
|
|
+ g2d.drawString(formattedDate, x, y);
|
|
|
+ g2d.dispose();
|
|
|
+ g2d.dispose();
|
|
|
+ ImageIO.write(outputImage, file.getName(), outputStream);
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(outputImage, "png", byteArrayOutputStream); // 这里可以根据需要指定格式
|
|
|
+ byte[] byteArray = byteArrayOutputStream.toByteArray();
|
|
|
+
|
|
|
+ MultipartFile multipart = new CustomMultipartFile(byteArray, file.getName(), file.getName(), "image/png");
|
|
|
+ try {
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ // 上传文件路径
|
|
|
+ String filePath = RuoYiConfig.getUploadPath();
|
|
|
+ String extension = getExtension(multipart);
|
|
|
+ MultipartFile multipartFile = null;
|
|
|
+ if (isPicture(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
|
|
|
+ multipartFile = imageMethod(multipart);
|
|
|
+ String thumbnail = FileUploadUtils.upload(filePath, multipartFile);
|
|
|
+ String url = serverConfig.getUrl() + thumbnail;
|
|
|
+ ajax.put("thumbnailUrl", url);
|
|
|
+ }
|
|
|
+ // 上传并返回新文件名称
|
|
|
+ String fileName = FileUploadUtils.upload(filePath, multipart);
|
|
|
+ String url = serverConfig.getUrl() + fileName;
|
|
|
+ if (url.indexOf("http") != -1) {
|
|
|
+ String replace = url.replace("http", "https");
|
|
|
+ url = replace;
|
|
|
+ }
|
|
|
+ ajax.put("fileName", multipart.getOriginalFilename());
|
|
|
+ ajax.put("url", url);
|
|
|
+ return ajax;
|
|
|
+ } catch (Exception e) {
|
|
|
return AjaxResult.error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
@@ -122,12 +188,9 @@ public class CommonController
|
|
|
*/
|
|
|
@GetMapping("/common/download/resource")
|
|
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
|
|
- throws Exception
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- if (!FileUtils.checkAllowDownload(resource))
|
|
|
- {
|
|
|
+ throws Exception {
|
|
|
+ try {
|
|
|
+ if (!FileUtils.checkAllowDownload(resource)) {
|
|
|
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
|
|
|
}
|
|
|
// 本地资源路径
|
|
@@ -139,9 +202,7 @@ public class CommonController
|
|
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
FileUtils.setAttachmentResponseHeader(response, downloadName);
|
|
|
FileUtils.writeBytes(downloadPath, response.getOutputStream());
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("下载文件失败", e);
|
|
|
}
|
|
|
}
|