首页>代码>SpringBoot集成OpenOffice实现Office文档在线预览>/springboot-office/src/main/java/com/simon/springbootoffice/controller/WebController.java
package com.simon.springbootoffice.controller;

import com.simon.springbootoffice.util.OpenOfficeUtl;
import org.jodconverter.office.OfficeException;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * @author Simon
 */
@Controller
public class WebController {
    /**
     * 跳转上传界面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String show() {
        return "upload";
    }

    /**
     * 路径加上 file=xx 查看pdf
     *
     * @return
     */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index() {
        return "viewer";
    }

    /**
     * 下载文件
     *
     * @param fileName
     * @param request
     * @param response
     */
    @RequestMapping(value = "download/{fileName}", method = RequestMethod.GET)
    public void index(@PathVariable("fileName") String fileName, HttpServletRequest request,
                      HttpServletResponse response) {
        try {
            File file = new File(ClassUtils.getDefaultClassLoader().getResource("static/").getPath() + fileName);
            FileChannel channel = new FileInputStream(file).getChannel();
            ServletOutputStream out = response.getOutputStream();
            WritableByteChannel channel_out = Channels.newChannel(out);
            channel.transferTo(0, file.length(), channel_out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws OfficeException
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file) throws OfficeException {
        File uploadFile = saveFile(file);
        File output = new File(ClassUtils.getDefaultClassLoader().getResource("static/").getPath() + System.currentTimeMillis() + ".pdf");
        OpenOfficeUtl.convert(uploadFile, output);
        return output.getName();
    }

    /**
     * 保存文件到本地
     *
     * @param file
     * @return
     */
    public File saveFile(MultipartFile file) {
        BufferedOutputStream out = null;
        try {
            File file1 = new File(file.getOriginalFilename());
            out = new BufferedOutputStream(new FileOutputStream(file1));
            out.write(file.getBytes());
            out.flush();
            return file1;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
最近下载更多
lironggang  LV38 2023年7月21日
小屁孩  LV7 2023年5月24日
ssmtest  LV5 2023年3月11日
crosa_Don  LV18 2023年3月2日
jk-mack  LV5 2023年2月19日
doutao6677  LV25 2023年2月10日
baize12345  LV1 2023年2月1日
Possess  LV4 2023年2月1日
最代码官方  LV167 2023年1月30日
最近浏览更多
DeanYao  LV2 2023年10月11日
zhaixunfei  LV8 2023年9月9日
cxy  LV1 2023年8月23日
woldxy  LV12 2023年8月22日
wang_d  LV12 2023年8月15日
dfkk_love  LV11 2023年7月27日
liqi666  LV7 2023年7月26日
爱吃鱼的猫Vital  LV6 2023年7月24日
lironggang  LV38 2023年7月21日
szf123  LV12 2023年5月31日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友