从CAMEL HTTP POST请求流媒体到一个文件 [英] Streaming to a file from an HTTP POST request in CAMEL

查看:1313
本文介绍了从CAMEL HTTP POST请求流媒体到一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为后续<一个href=\"http://stackoverflow.com/questions/33684618/using-camel-to-stream-output-from-a-post-to-a-url/33689401#33689401\">Using骆驼流从POST输出到网址我需要传输到使用CAMEL HTTP POST请求的文件,而无需等待发生的整个下载。我试过的URI:URI流:文件和URI:Bean组件和所有未能传输到文件逐字节,而是等待整个有效载荷来完成。我相信我一定要明智做错了什么配置,因为当文件被下载最终,它不这样做(从servlet的输出判断),直到输出的末尾。这是一个问题,因为该有效载荷是巨大的。我相信我应该尽快冲洗是从servlet做看到的文件输出,但是这不是发生了什么。

下面是路线:

 &LT;&路线GT;
    &LT;从URI =文件:文件名inputsDir = input.json&放大器;放大器;空操作=真正的/&GT;
    &LT;日志loggingLevel =INFO消息=$ {}体/&GT;
    &LT;到URI =HTTP://本地主机:8080 / ServingSource / FileServlet/&GT;
    &LT;到URI =豆:dloadBean方法= downloadFile/&GT;
    &LT;到URI =流:走出/&GT;
&LT; /路由&GT;

下面是叫的servlet:

 进口java.io.IOException异常;
进口java.io.StringWriter中;
进口java.nio.charset.StandardCharsets;
进口了java.util.Random;进口javax.servlet.ServletException;
进口javax.servlet.ServletOutputStream;
进口javax.servlet.annotation.WebServlet;
进口javax.servlet.http.HttpServlet;
进口javax.servlet.http.HttpServletRequest;
进口javax.servlet.http.HttpServletResponse;
进口javax.xml.bind.JAXBContext;
进口javax.xml.bind.JAXBException;
进口javax.xml.bind.Marshaller;进口org.apache.commons.lang3.RandomStringUtils;进口com.fasterxml.jackson.databind.ObjectMapper;/ **
 * Servlet实现类FileServlet
 * /
@WebServlet(/ FileServlet)
公共类FileServlet延伸的HttpServlet {
    私有静态最后的serialVersionUID长1L =;    / **
     * @see的HttpServlet#的HttpServlet()
     * /
    公共FileServlet(){
        超();
    }    / **
     * @see的HttpServlet#的doGet(HttpServletRequest的请求,HttpServletResponse的
     *响应)
     * /
    保护无效的doGet(HttpServletRequest的请求,HttpServletResponse的响应)
            抛出了ServletException,IOException异常{
        response.getWriter()追加(在放送:).append(request.getContextPath());。
    }    / **
     * @see的HttpServlet#的doPost(HttpServletRequest的请求,HttpServletResponse的
     *响应)
     * /
    保护无效的doPost(HttpServletRequest的请求,HttpServletResponse的响应)
            抛出了ServletException,IOException异常{
        ServletOutputStream的OUT = response.getOutputStream();
        ObjectMapper映射器=新ObjectMapper();
        工作人员输入= mapper.readValue(request.getInputStream(),Staff.class);
        字符串序言=&LT; XML版本= \\1.0 \\编码= \\UTF-8 \\独立= \\是\\&GT;?
        out.write(prolog.getBytes());
        串wrapperStart =下;包装&gt;中;
        out.write(wrapperStart.getBytes());
        JAXBContext中的JAXBContext = NULL;
        编组jaxbMarshaller = NULL;
        尝试{
            的JAXBContext = JAXBContext.newInstance(User.class);
            jaxbMarshaller = jaxbContext.createMarshaller();
        }赶上(JAXBException E1){
            e1.printStackTrace();
        }        尝试{
            随机随机=新的随机();
            的for(int i = 0; I&LT;千万,我++){
                INT ID = random.nextInt(3亿);
                用户的用户=新用户(ID,input.getName()+ input.getSalary()+ RandomStringUtils.randomAlphanumeric(10)。);
                jaxbMarshaller.setProperty(jaxb.fragment,Boolean.TRUE);
                StringWriter的作家=新的StringWriter();
                jaxbMarshaller.marshal(用户,作家);
                字符串编组= writer.toString()+\\ n;
                out.write(marshalled.getBytes(StandardCharsets.UTF_8));
                System.out.print(,+ I);
                如果(I%60 == 0){
                    的System.out.println();
                }
                    了out.flush();
            }
        }赶上(JAXBException E){
            e.printStackTrace();
        }        字符串wrapperEnd =&LT; /包装&gt;中;
        out.write(wrapperEnd.getBytes());
        out.close();
    }}

下面是下载的bean:

 包下载;进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;公共类DownloaderBean {
    私有静态最终诠释BUFFER_SIZE = 4096;    公共字符串downloadFile(的InputStream的InputStream)抛出IOException
        字符串文件名=findAll.xml;        字符串SaveDir可以=SaveDir可以;
        字符串SAVEFILEPATH = SaveDir可以+文件分割符+文件名;        FileOutputStream中的OutputStream =新的FileOutputStream(SAVEFILEPATH);        INT读取动作= -1;
        字节[]缓冲区=新的字节[BUFFER_SIZE];
        而((读取动作= inputStream.read(缓冲液))!= - 1){
            outputStream.write(缓冲液,0,读取动作);
        }        outputStream.close();
        inputStream.close();        返回文件下载;
    }}


解决方案

你为什么不使用骆驼网状要下载的文件?它与工作流完全。
http://camel.apache.org/netty-http.html

As a follow-up to Using Camel to stream output from a POST to a URL I need to stream to a file from an HTTP POST request using CAMEL without waiting for the entire download to occur. I tried the uri:stream uri:file and uri:bean components and all failed to stream to the file byte-by-byte but instead waited for the entire payload to finish. I believe I must be doing something wrong configuration wise because while the file is downloaded eventually, it isn't done (as judging from the output of the servlet) until the end of the output. This is a problem because the payload is huge. I believe I should be seeing output in the file as soon as the flush is done from the servlet, but that is not what's happening.

Here is the route:

<route>
    <from uri="file:inputsDir?fileName=input.json&amp;noop=true" />
    <log loggingLevel="INFO" message="${body}"/>
    <to uri="http://localhost:8080/ServingSource/FileServlet" />
    <to uri="bean:dloadBean?method=downloadFile" />
    <to uri="stream:out" />
</route>

Here is the servlet called:

import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.commons.lang3.RandomStringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class FileServlet
 */
@WebServlet("/FileServlet")
public class FileServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletOutputStream out = response.getOutputStream();
        ObjectMapper mapper = new ObjectMapper();
        Staff input = mapper.readValue(request.getInputStream(), Staff.class);
        String prolog = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
        out.write(prolog.getBytes());
        String wrapperStart = "<wrapper>";
        out.write(wrapperStart.getBytes());
        JAXBContext jaxbContext = null;
        Marshaller jaxbMarshaller = null;
        try {
            jaxbContext = JAXBContext.newInstance(User.class);
            jaxbMarshaller = jaxbContext.createMarshaller();
        } catch (JAXBException e1) {
            e1.printStackTrace();
        }

        try {
            Random random = new Random();
            for (int i = 0; i < 10000000; i++) {
                int id = random.nextInt(300000000);
                User user = new User(id, input.getName() + "." + input.getSalary() + RandomStringUtils.randomAlphanumeric(10));
                jaxbMarshaller.setProperty("jaxb.fragment", Boolean.TRUE);
                StringWriter writer = new StringWriter();
                jaxbMarshaller.marshal(user, writer);
                String marshalled = writer.toString() + "\n";
                out.write(marshalled.getBytes(StandardCharsets.UTF_8));
                System.out.print("," + i);
                if (i % 60 == 0) {
                    System.out.println("");
                }
                    out.flush();
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        String wrapperEnd = "</wrapper>";
        out.write(wrapperEnd.getBytes());
        out.close();
    }

}

Here is the downloader bean:

package download;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DownloaderBean {
    private static final int BUFFER_SIZE = 4096;

    public String downloadFile(InputStream inputStream) throws IOException {
        String fileName = "findAll.xml";

        String saveDir = "saveDir";
        String saveFilePath = saveDir + File.separator + fileName;

        FileOutputStream outputStream = new FileOutputStream(saveFilePath);

        int bytesRead = -1;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        outputStream.close();
        inputStream.close();

        return "File downloaded";
    }

}

解决方案

Why don't you use camel-netty to download the file? It works exclusively with streams. http://camel.apache.org/netty-http.html

这篇关于从CAMEL HTTP POST请求流媒体到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆