读取目标文件并从浏览器下载pdf文件

读取目标文件获取读取流,将读取流写入浏览器输出流,实现浏览器下载文件;

package com.jc.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

/**
 * 转pdf测试
 * @author wang-xiaoming
 *
 */
public class PdfTest {
    
    /**
     * 读取目标文件并从浏览器下载pdf文件
     * @param request 请求
     * @param response 响应
     * @param targetFile 目标文件名称
     * @param newFilename 生成文件名称
     */
    public static void downloadPdf(HttpServletRequest request, HttpServletResponse response, String targetFile, String newFilename){
        InputStream is = null;
        OutputStream os = null;
        try {
            File file = new File(targetFile);
            is = new FileInputStream(file);
            
            // 解决文件名乱码问题,获取浏览器类型,转换对应文件名编码格式,IE要求文件名必须是utf-8, firefo要求是iso-8859-1编码
            String agent = request.getHeader("user-agent");
            if (agent.contains("FireFox")) {
                newFilename = new String(newFilename.getBytes("UTF-8"), "iso-8859-1");
            } else {
                newFilename = URLEncoder.encode(newFilename, "UTF-8");
            }
            response.setHeader("Content-Disposition", "attachment;filename=" + newFilename + ".pdf");
            response.setHeader("content-type", "application/pdf");
//            response.addHeader("Content-Disposition", "attachment;filename=" + newFilename + ".xls");
//            response.setHeader("content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.flushBuffer();
            
            os = response.getOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            while((len = is.read(buffer)) > 0){
                os.write(buffer, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(os != null){
                    os.close();
                }
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 合并pdf文件
     * @param files
     * @param mergeFilename
     */
    public static void mergePdf(List<String> files, String mergeFilename){
        // 合并pdf
        OutputStream os = null;
        try {
            Document document = new Document(new PdfReader(files.get(0)).getPageSize(1));
            os = new FileOutputStream(mergeFilename);
            PdfCopy copy = new PdfCopy(document, os);
            document.open();
            
            int size = files.size();
            for (int i = 0; i < size; i++) {// 循环合并的PDF
                String pdfPath = files.get(i);
                PdfReader reader = new PdfReader(pdfPath);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {// 当前PDF页数
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            copy.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            try {
                if(os != null){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        List<String> files = new ArrayList<>(16);
        files.add("E:/11.pdf");
        files.add("E:/22.pdf");
        files.add("E:/33.pdf");
        mergePdf(files, "E:/112233.pdf");
    }
}

浏览器对输出文件流的请求头:

https://www.cnblogs.com/korea/p/11787460.html

 

上一篇:数学分析 H 1 复习要点(部分)(持续更新)


下一篇:【Html】网页编程的基础--HTML模板