|
@@ -4,10 +4,13 @@ package com.template.controller;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.template.common.utils.FileUtil;
|
|
import com.template.common.utils.FileUtil;
|
|
|
import com.template.model.result.CommonResult;
|
|
import com.template.model.result.CommonResult;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -17,9 +20,13 @@ import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
|
+import java.util.logging.Logger;
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequestMapping("/auto/upload")
|
|
@RequestMapping("/auto/upload")
|
|
|
|
|
+@Slf4j
|
|
|
public class UploadServlet {
|
|
public class UploadServlet {
|
|
|
|
|
|
|
|
@Value("${image.url}")
|
|
@Value("${image.url}")
|
|
@@ -28,32 +35,67 @@ public class UploadServlet {
|
|
|
@Value("${image.path}")
|
|
@Value("${image.path}")
|
|
|
private String imagePath;
|
|
private String imagePath;
|
|
|
|
|
|
|
|
|
|
+// @PostMapping("/save")
|
|
|
|
|
+// protected CommonResult doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
+// // 获取文件内容
|
|
|
|
|
+// Part filePart = request.getPart("file");
|
|
|
|
|
+// InputStream fileContent = filePart.getInputStream();
|
|
|
|
|
+//
|
|
|
|
|
+// String fileNameWithPath = FileUtil.getFileNameWithPath() + ".jpg";
|
|
|
|
|
+//
|
|
|
|
|
+//// 线下
|
|
|
|
|
+//// FileUtil.makeDirs(fileNameWithPath, "E://image3");
|
|
|
|
|
+//// String imgFilePath = "E://image3/" + fileNameWithPath;
|
|
|
|
|
+//
|
|
|
|
|
+//// 线上
|
|
|
|
|
+// FileUtil.makeDirs(fileNameWithPath, imagePath);
|
|
|
|
|
+// String imgFilePath = imagePath+"/" + fileNameWithPath;
|
|
|
|
|
+//
|
|
|
|
|
+//// 上传图片
|
|
|
|
|
+// Files.copy(fileContent,new File(imgFilePath).toPath());
|
|
|
|
|
+//
|
|
|
|
|
+// // 保存文件到指定目录
|
|
|
|
|
+//// String fileName = filePart.getSubmittedFileName();
|
|
|
|
|
+//// OutputStream out = new FileOutputStream(imgFilePath);
|
|
|
|
|
+//// byte[] buffer = new byte[1024];
|
|
|
|
|
+//// int bytesRead;
|
|
|
|
|
+//// while ((bytesRead = fileContent.read(buffer)) != -1) {
|
|
|
|
|
+//// out.write(buffer, 0, bytesRead);
|
|
|
|
|
+//// }
|
|
|
|
|
+//// out.close();
|
|
|
|
|
+// String url=imageUrl+"/image/"+fileNameWithPath;
|
|
|
|
|
+// log.info("上次的图片链接:{}",url);
|
|
|
|
|
+//
|
|
|
|
|
+//// response.getWriter().println(url);
|
|
|
|
|
+// JSONObject jsonObject = new JSONObject();
|
|
|
|
|
+// jsonObject.put("url",url);
|
|
|
|
|
+// return CommonResult.ok(jsonObject);
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
@PostMapping("/save")
|
|
@PostMapping("/save")
|
|
|
- protected CommonResult doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
- // 获取文件内容
|
|
|
|
|
- Part filePart = request.getPart("file");
|
|
|
|
|
- InputStream fileContent = filePart.getInputStream();
|
|
|
|
|
-
|
|
|
|
|
- String fileNameWithPath = FileUtil.getFileNameWithPath() + ".jpg";
|
|
|
|
|
-// FileUtil.makeDirs(fileNameWithPath, "E://image3");
|
|
|
|
|
-// String imgFilePath = "E://image3/" + fileNameWithPath;
|
|
|
|
|
- FileUtil.makeDirs(fileNameWithPath, imagePath);
|
|
|
|
|
- String imgFilePath = imagePath+"/" + fileNameWithPath;
|
|
|
|
|
-
|
|
|
|
|
-// 上传图片
|
|
|
|
|
- Files.copy(fileContent,new File(imgFilePath).toPath());
|
|
|
|
|
-
|
|
|
|
|
- // 保存文件到指定目录
|
|
|
|
|
-// String fileName = filePart.getSubmittedFileName();
|
|
|
|
|
-// OutputStream out = new FileOutputStream(imgFilePath);
|
|
|
|
|
-// byte[] buffer = new byte[1024];
|
|
|
|
|
-// int bytesRead;
|
|
|
|
|
-// while ((bytesRead = fileContent.read(buffer)) != -1) {
|
|
|
|
|
-// out.write(buffer, 0, bytesRead);
|
|
|
|
|
-// }
|
|
|
|
|
-// out.close();
|
|
|
|
|
- String url=imageUrl+"/image/"+fileNameWithPath;
|
|
|
|
|
-// response.getWriter().println(url);
|
|
|
|
|
|
|
+ protected CommonResult doPost(@RequestParam("file") MultipartFile file) throws ServletException, IOException {
|
|
|
|
|
+ // 1. 校验空文件
|
|
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
|
|
+ return CommonResult.fail("上传文件不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 保留原始扩展名,无扩展名时默认 .jpg
|
|
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
|
|
+ String ext = (originalFilename != null && originalFilename.contains("."))
|
|
|
|
|
+ ? originalFilename.substring(originalFilename.lastIndexOf("."))
|
|
|
|
|
+ : ".jpg";
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 生成文件名 + 确保目录存在
|
|
|
|
|
+ String fileNameWithPath = FileUtil.getFileNameWithPath() + ext;
|
|
|
|
|
+ Path targetPath = Paths.get(imagePath, fileNameWithPath);
|
|
|
|
|
+ Files.createDirectories(targetPath.getParent());
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 写入文件(transferTo 内部自动关闭流)
|
|
|
|
|
+ file.transferTo(targetPath.toFile());
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 返回可访问 URL
|
|
|
|
|
+ String url = imageUrl + "/image/" + fileNameWithPath;
|
|
|
|
|
+ log.info("上传图片链接:{}", url);
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.put("url",url);
|
|
jsonObject.put("url",url);
|
|
|
return CommonResult.ok(jsonObject);
|
|
return CommonResult.ok(jsonObject);
|