文件上传 word转pdf

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();
    }


}

猜你喜欢

转载自blog.csdn.net/kevin_cat/article/details/83242931