自学内容网 自学内容网

文件上传生成pdf

前端

后端
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);
            }
        }
    }

原文地址:https://blog.csdn.net/goldfishsky/article/details/145174504

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!