前端
后端
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
@RequestMapping("/api/pdf")
public class testController {
@PostMapping("/generate1")
public void generatePdf1(){
System.out.println("1111");
}
@PostMapping("/generate")
public ResponseEntity<String> generatePdf(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return new ResponseEntity<>("File is empty", HttpStatus.BAD_REQUEST);
}
try {
// 定义保存文件的路径
String uploadDir = "D:\\project\\";
File directory = new File(uploadDir);
if (!directory.exists()) {
directory.mkdirs(); // 创建目录
}
// 保存文件到指定路径
String filePath = uploadDir + "111.pdf";
File destFile = new File(filePath);
file.transferTo(destFile);
return new ResponseEntity<>("File uploaded successfully: " + filePath, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<>("Error saving file", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}