从数据库

时间:2016-11-06 12:33:31

标签: tomcat java-ee browser blob xlsx

我正在尝试打开一个xlsx文件,它实际上是作为Blob存储在数据库中的。所以这个过程分为三个步骤:

1st:从数据库中获取文件。

第二名:将其保存在服务器上。

第3名:打开它。

我尝试使用 outputstream ,如下所示:

 InputStream in = blob.getBinaryStream();
 OutputStream out = new FileOutputStream("c:/mytemp/testanar.xlsx");
 byte[] buff = blob.getBytes(1,(int)blob.length());
 out.write(buff);
 out.close();

但是我找不到任何与URL一起使用此方法的问题。

我的问题是如何将文件保存在服务器上?

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我的问题是找到一种方法来使用Web应用程序中的浏览器打开Excel文件。 这是两个步骤的解决方案:

第一步:

安装一个扩展程序,帮助您阅读我使用https://chrome.google.com/webstore/detail/office-editing-for-docs-s/gbkeegbaiigmenfmjfclcdgdpimamgkj

的excel文件

第二步:

创建一个帮助您阅读文件的servlet是我的一部分:

public String downloadDocument(@PathVariable int docId, HttpServletResponse response) throws IOException {
    Rapport document = rapportService.findRapportById(docId);

    response.setContentType(document.getType());
    response.setContentLength(document.getContent().length);
    // if you want to download the file instead of reading the same file 
    // remove "//" below
    //response.setHeader("Content-Disposition", "attachment; filename=\"" + document.getName() + "\"");
    FileCopyUtils.copy(document.getContent(), response.getOutputStream());

    return "redirect:listofreports";
}

[编辑] : 其中Rapport是表示excel文件的模型。

祝你好运。

相关问题