0

この例に従って、ユーザーが pdf をダウンロードできるようにしました。

ヴァーディンの例

/** 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 に答える