diff --git a/src/main/java/qiwu/xfllmdemo/controller/PreGenerateController.java b/src/main/java/qiwu/xfllmdemo/controller/PreGenerateController.java new file mode 100644 index 0000000..025d92d --- /dev/null +++ b/src/main/java/qiwu/xfllmdemo/controller/PreGenerateController.java @@ -0,0 +1,51 @@ +package qiwu.xfllmdemo.controller; + +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; + +@Controller +// @RestMapping +@RequestMapping("/pregen") +public class PreGenerateController { + // @PostMapping("/upload") + @RequestMapping(value = "/upload", method = RequestMethod.POST) + public void uploadFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) { + if (file.isEmpty()) { + // 处理文件为空的情况 +// return "redirect:/fileEmpty"; + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); // 400 + System.out.println("uploadFile emptyFile"); + } + try { + // 定义文件存储路径,可以根据需求更改 + System.out.println("uploadFile try"); + String uploadDir = "~/uploadedFile"; // 存储文件的目录 + String fileName = "uploadedUserWords.txt"; // file.getOriginalFilename(); // 获取上传文件的原始文件名 + // 创建存储目录(如果不存在) + File uploadPath = new File(uploadDir); + if (!uploadPath.exists()) { + uploadPath.mkdirs(); + } + // 构建文件存储路径 + String filePath = uploadDir + File.separator + fileName; + // 将文件保存到服务器本地 + file.transferTo(new File(filePath)); + // 文件上传成功,可以进行其他业务逻辑处理 +// return "redirect:/uploadSuccess"; + response.setStatus(HttpServletResponse.SC_OK); // 200 + System.out.println("uploadFile OK"); + } catch (IOException e) { + // 处理文件上传异常 +// return "redirect:/uploadFailure"; + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } +} + diff --git a/src/main/resources/static/pre-generate.html b/src/main/resources/static/pre-generate.html index c32afcc..ab4ddeb 100644 --- a/src/main/resources/static/pre-generate.html +++ b/src/main/resources/static/pre-generate.html @@ -9,12 +9,12 @@
-在此处上传预设用户话语txt
- - - +在此处上传预设用户话语txt
+ + + - - + +