文件上传 word转pdf

本文介绍了一个使用Spring框架实现的文件上传控制器,演示了如何从客户端接收文件并将其保存到服务器,同时展示了如何利用LibreOffice将上传的文档转换为PDF格式的方法。文章深入探讨了文件读写操作及系统命令调用的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;

@Controller
public class FileUploadController {

    @RequestMapping("/yl/action")
    public String yl_action(Map<String,Object> map, @RequestParam String path) throws IOException {
        String targetPath="src/main/resources/upload";
        String command = "/Applications/LibreOffice.app/Contents/MacOS/soffice --convert-to pdf  -outdir " + targetPath + " " + path;;
        Runtime.getRuntime().exec(command);
        map.put("url","/upload/demo.pdf");
        return "yl_ac";
    }


    @RequestMapping("/yl/index")
    public String yl(){
        return "yl";
    }


    @RequestMapping("/upload/action")
    public String up(@RequestParam MultipartFile file) throws IOException {
        SaveFileFromInputStream(file.getInputStream(),"/Users/zhangmuqing/Desktop/","test.jpeg");
       return "ok";
    }


    @RequestMapping("/upload/index")
    public String index(){
        return "index";
    }

    public void SaveFileFromInputStream(InputStream is,String path,String filename) throws IOException {
        File newFile = new File(path);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        OutputStream os = new FileOutputStream(path+filename);
        int len = 0;
        byte[] buffer =new byte[1024*1024];
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        os.close();
        is.close();
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值