package com.zjrs.ggfw.sggfw.util;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.Base64;


public class DownloadFile {
    public static void download(String attachments,HttpServletResponse response){
        OutputStream outputStream =null;
        try {
            response.reset();
            byte[] data = Base64.getMimeDecoder().decode(attachments);
            String fileName="download.pdf";
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.addHeader("Content-Length", "" + data.length);
            outputStream = new BufferedOutputStream(response.getOutputStream());
            outputStream.write(data);
            outputStream.flush();
            response.flushBuffer();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(outputStream!=null){
                    outputStream.close();
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
