简体   繁体   English

Java EE中的Web数据流

[英]Web Data Streaming in Java EE

we have a design like this : 我们有这样的设计:

在此输入图像描述

our installed service upon tomcat has a method getDatabaseData(String request) (RMI Client) which receive data from database using (RMI Server implementation) existing on the core. 我们在tomcat上安装的服务有一个方法getDatabaseData(String request)(RMI Client),它使用核心上存在的(RMI Server实现)从数据库接收数据。

we want to do something that our installed service which is inside tomcat make a xml file and send it to client now : 我们想要做一些事情,我们在tomcat中安装的服务生成一个xml文件并立即发送给客户端:

we have thought about a solution that : 我们考虑过一个解决方案:

  • first make the whole xml file on our tomcat using the getDatabaseData(String request) & then our user can get a reference using a link to it & start downloading the data. 首先使用getDatabaseData(String请求)在我们的tomcat上创建整个xml文件然后我们的用户可以使用链接获取引用并开始下载数据。

but this solution is not informative for us because the size of this file is so big and if we want to do this our tomcat server storage will get full so fast & get down, so we're looking for a solution for real time streaming data to user instead of creating the whole file. 但是这个解决方案对我们来说没有信息,因为这个文件的大小是如此之大,如果我们想要这样做,我们的tomcat服务器存储将变得如此之快和下降,所以我们正在寻找实时流数据的解决方案用户而不是创建整个文件。 I mean a way to send the data to user in a xml template when data is receiving from the CORE. 我的意思是当从CORE接收数据时,在xml模板中将数据发送给用户的方法。

Do you have any idea ? 你有什么主意吗 ?

(by the way the web service upon tomcat has been written using Spring MVC & Spring Security) (通过使用Spring MVC和Spring Security编写tomcat上的Web服务)

I can give you a pice of code from my current project, where we return a file in struts action: 我可以从我当前的项目中给你一些代码,我们在struts动作中返回一个文件:

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
ServletOutputStream out = response.getOutputStream();
out.write(file);

Is this what you are looking for? 这是你想要的?

using @Noofiz code, I wrote this code for testing this functionality & that worked for me! 使用@Noofiz代码,我编写了这段代码来测试这个功能并且对我有用!

we can read from a file and send it out on-time to user and let the user download the file ! 我们可以从文件中读取并按时发送给用户,让用户下载文件!

1) I printed 'End!' 1)我打印'结束!' to make sure that the file is sending to user on time when It's buffering and not finish buffering first & then send it to user ! 确保文件在缓冲时按时发送给用户,而不是先完成缓冲,然后发送给用户!

2) the downloaded file & main file md5 checked and they were similar. 2)检查下载的文件和主文件md5,它们是相似的。

here is my servlet code : 这是我的servlet代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    InputStream is = new FileInputStream("/home/Mehdi/Desktop/log.log");
    response.setContentType("application/octet-stream");
    ServletOutputStream out = response.getOutputStream();
    int i;
    byte[] buffer = new byte[10 * 1024];

    while ((i = is.read(buffer)) != -1) {
        if (i != 10240) {
            for (int j = 0; j < i; j++) {
                out.write(buffer[j]);
            }
        } else {
            out.write(buffer);
        }
    }
    System.out.println("End!");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM