springboot 上传图片到指定位置 并保存到数据库

**
自己在开发项目的时候,卡在了上传图片的地方,为了以后可以轻松开发特地记录,也希望看见次文章的人能够少走弯路

废话不多说,上代码

@PostMapping("/upfile/image")
    @ResponseBody
    public ResultDTO updateSource(@RequestParam("file") MultipartFile file){
        AbnormalOrder abnormalOrder=new AbnormalOrder();
       // abnormalOrder.setReason(reason);
        //给予文件新的随机名字
         String newname=null;
         String oldname= file.getOriginalFilename();
          newname= UUID.randomUUID().toString()+ oldname.substring(oldname.lastIndexOf("."));
          //创建存放路径的文件
        File upload = new File("G:/back-server/images/upload/");
        if(!upload.exists()){
            upload.mkdirs();
        try {
            //将图片存放到指定的文件里面
          File  resultpath=  new File(upload+newname);
            file.transferTo(resultpath);
            String finalpath=resultpath.toString();
            abnormalOrder.setImage(finalpath);
        } catch (IOException e) {
            e.printStackTrace();
          ResultDTO resultDTO=new ResultDTO(ResultCodeEnum.IMPORTSUCESS);
          resultDTO.setData(abnormalOrder);
          return  resultDTO;

另外
在测试的时候
我只写了上传图片的

希望可以帮助大家!

**springboot 上传图片到指定位置 并保存到数据库**自己在开发项目的时候,卡在了上传图片的地方,为了以后可以轻松开发特地记录,也希望看见次文章的人能够少走弯路废话不多说,上代码@PostMapping("/upfile/image") @ResponseBody public ResultDTO updateSource(@RequestParam("file") MultipartFile file){ AbnormalOrder abnormalOr
问题:loadrunner提示System.err: log4j:WARN No appenders could be found for logger 问题原因:jar包的位置引入错误导致,修改jar包位置即可 修改jar包对应位置: 运行脚本,问题解决: 希望可以帮助大家 private static String uploadFilePath; /** 上传路径-windows */ private static String uploadWindowsFilePath; /** 上传路径-linux */ private stat
初次接触SpringBoot的小伙伴,对于前后端合并在一起的小项目,静态资源的放置可能有一丢丢疑惑。 其实非常简单,这里简单举个例子方便大家理解 前后端合并的SpringBoot项目,其静态资源都放在src/main/resource目录下 而resources目录又可以根据具体的使用途径分为的public、static、template 等目录 再进入html 页面 看一下具体的使用路径 由于我在template 文件夹下放置了 html 文件,所以这里的路径就是 先用 .. 返回上一级-->
实现方式说明: 该业务场景有多种实现方式,我们根据不同的场景选择合适的实现方式。本篇主要介绍的是图片由前端调用接口传递给后端后,由后端直接保存数据库保存,读取时后端直接将图片以流的方式返回给前端来实现; 本篇只列举后端代码,保存图片时既保存了原图,也保存了缩略图; 实现步骤: 1、前期准备 1.1 创建可以保存图片的表结构 图片是以blob的类型在数据库进行保存的,考虑到原图有可能会比较大,所以采用了longbl
可以使用Apache PDFBox和Spring Boot的MultipartFile类来实现PDF图片上传到服务器并保存路径到数据库。首先,使用MultipartFile类将PDF文件上传到服务器上的指定目录。然后,使用Apache PDFBox将PDF文件转换为图片,并将图片保存到服务器上的指定目录。最后,将图片路径保存数据库中。以下是示例代码: @PostMapping("/uploadPDF") public String uploadPDF(@RequestParam("file") MultipartFile file) { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); String uploadDir = "pdf_uploads"; String filePath = uploadDir + "/" + fileName; try { FileUploadUtil.saveFile(uploadDir, fileName, file); PDDocument document = PDDocument.load(new File(filePath)); PDFRenderer pdfRenderer = new PDFRenderer(document); BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB); String imageDir = "image_uploads"; String imageName = FilenameUtils.getBaseName(fileName) + ".png"; String imagePath = imageDir + "/" + imageName; FileUploadUtil.saveImage(imageDir, imageName, bim); document.close(); // 将图片路径保存数据库中 // ... return "File uploaded successfully!"; } catch (IOException e) { e.printStackTrace(); return "Failed to upload file!"; 其中,FileUploadUtil是一个工具类,用于保存文件和图片指定目录: public class FileUploadUtil { public static void saveFile(String uploadDir, String fileName, MultipartFile file) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); try (InputStream inputStream = file.getInputStream()) { Path filePath = uploadPath.resolve(fileName); Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new IOException("Could not save file: " + fileName, e); public static void saveImage(String uploadDir, String imageName, BufferedImage image) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); Path imagePath = uploadPath.resolve(imageName); ImageIO.write(image, "png", imagePath.toFile()); Remote URL test failed: remote: [31m18617642193: Incorrect username or password (access token)[0m List、Set区别、ArrayList、LinkedList、HashSet、TreeSet区别以及HashMap、Hashtable、ConccurentHashMap三者的区别: RuntimeException和非RuntimeException、Error Spring、SpringMVC、SpringBoot区别、Maven的构建生命周期