作为Using Camel to stream output from a POST to a URL的后续内容,我需要使用CAMEL从HTTP POST请求流式传输到文件,而无需等待整个下载发生。我尝试了uri:stream uri:file和uri:bean组件,并且都无法逐字节地传输到文件,而是等待整个有效负载完成。我相信我必须做一些错误的配置,因为最终下载文件时,它不会完成(从servlet的输出判断)直到输出结束。这是一个问题,因为有效载荷很大。我相信我应该在从servlet完成刷新后立即看到文件中的输出,但这不是正在发生的事情。
这是路线:
<route>
<from uri="file:inputsDir?fileName=input.json&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>
这是一个名为:
的servletimport 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();
}
}
这是下载器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";
}
}