Javaプレビューpdfファイル

8127 ワード

package com.ctdcn.business.pdfInfo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
public class demo {

    @GetMapping("showpdf")
    public String showpdf(HttpServletResponse response) throws IOException {
        //    
        InputStream in = new FileInputStream("C:\\ctdFile\
ewPdf\\ab784ec2-6c25-4186-9351-18ad5e61d9be-NZ-CNS-9.pdf"
); byte[] data = toByteArray(in); if(data.length == 0 && data == null){ return " "; } // response.setContentType("application/pdf"); OutputStream out = response.getOutputStream(); out.write(data); return "ok"; } /** * * @param in * @return * @throws IOException */ private byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 4]; int n = 0; while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); } byte[] bytes = out.toByteArray(); return bytes; } }