从服务器下载PDF文件

时间:2013-07-17 10:11:14

标签: java

我需要将某些数据转换为pdf文件。为此我已经使用以下代码来保存服务器中TablePdf.pdf中的数据。 (这里pdf文件保存在C:\目录中)

public String generatePdf() throws Exception
{
Font font = FontFactory.getFont("Ms Dialog Light");
BaseFont pdfFont = font.getBaseFont();

// TODO Auto-generated method stub
HashMap inputMap = new HashMap();

inputMap.put(TableProperties.PDF_PATH, "c://TablePdf.pdf");
inputMap.put(TableProperties.PDF_TABLE_NAME, "Table");

inputMap.put(TableProperties.PDF_HEIGHT, "1000");
inputMap.put(TableProperties.PDF_WIDTH, "1500");

ArrayList<String> columnNameList = new ArrayList<String>();
ArrayList<String> dataList = new ArrayList<String>();
ArrayList<String> columnWidthList = new ArrayList<String>();

columnNameList.add("Col1");
columnNameList.add("Col2");
columnNameList.add("Col3");
columnNameList.add("Col4");
columnNameList.add("Col5");

columnWidthList.add("1");
columnWidthList.add("2");
columnWidthList.add("2");
columnWidthList.add("3");
columnWidthList.add("1");

for (int i = 0; i < 9; i++)
    {
    dataList.add("Id" + i);
    dataList.add("Name is = " + Math.random() * i);
    dataList.add("Field Value1 is = " + Math.random() * i);
    dataList.add("Field Value2 is = " + Math.random() * i);
    dataList.add("Field Value3 is = " + Math.random() * i);
    }

inputMap.put(TableProperties.PDF_TABLE_COLUMN_NUMBER, "5");
inputMap.put(TableProperties.PDF_TABLE_COLUMN_NAME, columnNameList);
inputMap.put(TableProperties.PDF_TABLE_COLUMN_VALUES, dataList);

inputMap.put(TableProperties.PDF_TABLE_HEADER_WIDTH, columnWidthList);

inputMap.put(TableProperties.PDF_HEADER, "            Hello\n\n");
inputMap.put(TableProperties.PDF_HEADER_FONT_NAME, pdfFont);
inputMap.put(TableProperties.PDF_HEADER_FONT_SIZE, "20.0");
inputMap.put(TableProperties.PDF_HEADER_ALIGNMENT, Element.ALIGN_LEFT);

inputMap.put(TableProperties.PDF_FOOTER, "             Tata");
inputMap.put(TableProperties.PDF_FOOTER_FONT_NAME, pdfFont);
inputMap.put(TableProperties.PDF_FOOTER_FONT_SIZE, "9.0");
inputMap.put(TableProperties.PDF_FOOTER_ALIGNMENT, Element.ALIGN_RIGHT);

inputMap.put(TableProperties.PDF_TABLE_CELL_HEIGHT, "6.0");
inputMap.put(TableProperties.PDF_TABLE_HEADER_HEIGHT, "4.0");

inputMap.put(TableProperties.PDF_TABLE_ALTERNATE_BACKGROUND_COLOR, "Y");
inputMap.put(TableProperties.PDF_TABLE_BACKGROUND_COLOR, BaseColor.CYAN);
inputMap.put(TableProperties.PDF_TABLE_CELL_ALIGNMENT, new Integer(Element.ALIGN_LEFT));
inputMap.put(TableProperties.PDF_TABLE_FONT_NAME, pdfFont);
inputMap.put(TableProperties.PDF_TABLE_FONT_SIZE, "6.0");
inputMap.put(TableProperties.PDF_TABLE_HEADER_ALIGNMENT, new Integer(Element.ALIGN_CENTER));
inputMap.put(TableProperties.PDF_TABLE_HEADER_BACKGROUND_COLOR, BaseColor.GRAY);

inputMap.put(TableProperties.PDF_TABLE_HEADER_FONT_NAME, FontFactory.getFont("Times-Roman").getBaseFont());
inputMap.put(TableProperties.PDF_TABLE_HEADER_FONT_SIZE, "6.0");

CreateTable crtTbl = new CreateTable();
    crtTbl.createTable(inputMap);
}

现在我需要允许客户端以便他们可以下载pdf文件。

-------------------- EDITED -------------------------- ------

下面是我下载pdf文件的jsp代码。它在控制台中没有出错,但文件没有下载。

<%@ page import="java.util.*,java.io.*"%>
<%@ page language="java"%>

<%
    try
    {

        response.setContentType ("application/pdf");    
        //set the header and also the Name by which user will be prompted to save
        response.setHeader ("Content-Disposition", "attachment;filename=TablePdf.pdf");

        File f = new File ("C:\\TablePdf.pdf");

        InputStream inputStream = new FileInputStream(f);
        ServletOutputStream servletOutputStream = response.getOutputStream();
        int bit = 256;
        int i = 0;
        try 
        {

            while ((bit) >= 0) 
            {
                bit = inputStream.read();
                servletOutputStream.write(bit);
            }
            System.out.println("" +bit);


            }
            catch (Exception ioe) 
            {
                ioe.printStackTrace(System.out);
            }
            servletOutputStream.flush();
            //outs.close();
            inputStream.close();    
    }
    catch(Exception e)
    {

    }

%>

1 个答案:

答案 0 :(得分:2)

有很多选择。其中两个:

  • 安装一个简单的Apache服务器 - 将PDF文件存储在htdocs下,并且可以访问它们
  • 拥有tomcat(或其他servlet容器),并创建一个servlet,从其存储的目录中读取文件并将其流式传输以供下载。简而言之,这是通过将其字节从FileInputStream转移到response.getOutputStream()来完成的。同时设置Content-Disposition`标题