0

我按照这个例子让用户下载 pdf :

Vaadin 示例

/** Copied from example */
@Override
protected void init(VaadinRequest request) {
    Button downloadButton = new Button("Download image");

    StreamResource myResource = createResource();
    FileDownloader fileDownloader = new FileDownloader(myResource);
    fileDownloader.extend(downloadButton);

    setContent(downloadButton);
}

/** modified from example */
private StreamResource createResource() throws IOException {
    return new StreamResource(new StreamSource() {
        byte[] bytes =loadFile("/home/amira/Desktop/repTest.pdf"); //Get the file bytes here
        InputStream is = new ByteArrayInputStream(bytes);
        @Override
        public InputStream getStream() {
            return is ;
        }
    }, "report.pdf");
}

public static byte[] readFully(InputStream stream) throws IOException {
    byte[] buffer = new byte[8192];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int bytesRead;
    while ((bytesRead = stream.read(buffer)) != -1) {
        baos.write(buffer, 0, bytesRead);
    }
    return baos.toByteArray();
}

public static byte[] loadFile(String sourcePath) throws IOException {
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(sourcePath);
        return readFully(inputStream);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

当我尝试打开文件时,它说文件损坏

4

1 回答 1

1

请参阅我的工作示例(针对 20Mb PDF 进行测试):

private void downloadDocument()
{
    final String retrievalName = "222.pdf";

    class FileDownloadResource extends FileResource
    {
        public FileDownloadResource(File sourceFile, Application application)
        {
            super(sourceFile, application);
        }

        public DownloadStream getStream()
        {
            try
            {
                byte[] DocContent = null;
                DocContent = getFileBytes("C:\\Temp\\222.pdf");
                if (DocContent != null)
                {
                    final DownloadStream ds = new DownloadStream(new ByteArrayInputStream(DocContent), "application/pdf", retrievalName);
                    ds.setCacheTime(getCacheTime());
                    String fileName = URLEncoder.encode(retrievalName, "UTF8");
                    // force download!
                    ds.setParameter("Content-Disposition", "attachment; filename*=\"utf-8'" + fileName + "\"");
                    return ds;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e1)
            {
                e1.printStackTrace();
                return null;
            }
        }
    }
    getApplication().getMainWindow().open(new FileDownloadResource(new File(retrievalName), getApplication()));
}


/**
 * getFileBytes
 * 
 * @author NBochkarev
 * 
 * @param fileOut
 * @return
 * @throws IOException
 */
public static byte[] getFileBytes(String fileName) throws IOException
{
    ByteArrayOutputStream ous = null;
    InputStream ios = null;
    try
    {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        ios = new FileInputStream(new File(fileName));
        int read = 0;
        while ((read = ios.read(buffer)) != -1)
            ous.write(buffer, 0, read);
    }
    finally
    {
        try
        {
            if (ous != null)
                ous.close();
        }
        catch (IOException e)
        {
            // swallow, since not that important
        }
        try
        {
            if (ios != null)
                ios.close();
        }
        catch (IOException e)
        {
            // swallow, since not that important
        }
    }
    return ous.toByteArray();
}
于 2013-07-09T05:56:23.200 回答