I’d like to be able to write an entire request to a file within a Spring MVC controller.
I’ve tried the following, but the file is always empty even though I’m making a POST request with loads of parameters:
@RequestMapping(method = RequestMethod.POST, value = "/payments/confirm")
public void receiveCallback(ServletInputStream inputStream)
{
try
{
inputStream.reset();
byte[] data = IOUtils.toByteArray(inputStream);
File file = new File(System.getProperty("java.io.tmpdir") + "test" + System.currentTimeMillis() + ".txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
}
catch (Exception e)
{
logger.error("Error writing request", e);
}
}
I’ve also tried using HttpServletRequest.getInputStream(), but the same results.
Using the InputStream won’t work (see BalusC’s answer). Here’s a sample of how you could use a HTTPServletRequest object instead to write headers and parameters: