Просмотр файлов в веб-приложении j2ee с помощью fancybox и Google Doc Viewer - PullRequest
1 голос
/ 09 июня 2019

У меня есть сервлет Java, который обслуживает файлы из s3. Эти файлы могут быть изображения, PDF или DOC / DOCX. Мне нужно реализовать функцию в моем веб-приложении для отображения этих файлов в Google Doc Viewer.

В моем случае я не могу отправить контент, так что Google Doc Viewer читает его и отображает файл. (когда следует загрузить == Binary.YES).

Это код, который у меня есть, который загружает файл из s3 и устанавливает его в моем классе бина -

ApplicantzFileModel document = (ApplicantzFileModel) MongoSelectRogueModel
                .selectOneById(ApplicantzFileModel.class, id);
        Binary shouldDownload = Binary.valueOf(download);
        if (document != null) {
            if (shouldDownload == Binary.YES) {
                docBean.fileStream = document.downloadFileStream();
                docBean.fileName = document.getFileName();
                docBean.isFileDownloadResponse = true;
            } else {
                File file = document.downloadFile();
                if (file != null) {
                    String content = null;
                    content = FileHandler.read(file);
                    if (content != null) {
                        docBean.fileContent = content;
                        docBean.fileName = document.getFileName();
                    }
                }
            }
            docBean.actionSuccess = true;
            docBean.isFileContentResponse = true;
        }

Это код в моем doGet / doPost для ответа -

if (bean.isFileDownloadResponse) {
                    OutputStream responseOutputStream;
                    try {
                        response.setContentType("application/octet-stream");
                        response.addHeader("Content-disposition", "inline; filename=" + bean.fileName);
                        responseOutputStream = response.getOutputStream();
                        byte[] buf = new byte[4096];
                        int len = -1;
                        while ((len = bean.fileStream.read(buf)) != -1) {
                            responseOutputStream.write(buf, 0, len);
                        }
                        responseOutputStream.flush();
                        responseOutputStream.close();
                        bean.fileStream.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } else {
                    response.setContentType("application/msword");
                    response.addHeader("Content-disposition", "inline; filename=" + bean.fileName);
                    responseStr = bean.fileContent;
                    PrintWriter writer = null;
                    try {
                        writer = response.getWriter();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (writer != null) {
                        writer.println(responseStr);
                        writer.flush();
                        writer.close();
                    }
                }

И это HTML-код в моем интерфейсе, чтобы открыть Google Doc Viewer в Fancybox -

"<a data-fancybox="" data-type="iframe" data-src="http://docs.google.com/viewer?url=http%3A%2F%2Fsomeserver.com%2Fapplication%2Fdocument%3Faction%3Dget%26id%3Dsomeid%26download%3DYES&amp;embedded=true" href="javascript:void(0);" data-doc-id="someid">My Link</a>"

Или, точнее, URL Google Viewer, который открывается -

var innerurl = encodeURIComponent('http://someserver.com/application/document?action=get&id='+this.docId+'&download=YES');
        this.docUrl = 'http://docs.google.com/viewer?url='+innerurl+'&embedded=true';

Я не могу открыть документ в формате Word или PDF в своей коробке. Может кто-нибудь подсказать, пожалуйста, что делать для обслуживания файлов через Java.

...