0

I am new to Spring MVC and trying to see if this is possible. So if I make a single request and this request takes about 1 minute to process before writing to the OutputStream from the HttpServletResponse object, then I make another request that only takes 15 seconds and writes to the OutputStream. Do each request share the same OutputStream? Currently I am getting an error and I am thinking its because the second request closes the OutputStream. This is the error "java.net.SocketException: Software caused connection abort: socket write error"

private static final String APPLICATION_EXCEL = "application/vnd.ms-excel";

@RequestMapping(value = "/a", method = RequestMethod.GET, produces = APPLICATION_EXCEL)
public @ResponseBody
void downloadA(HttpServletResponse response) throws IOException {
    response.setContentType(APPLICATION_EXCEL);
    response.setHeader("Content-Disposition", "attachment; filename=" + "a.xlsx");
    HSSFWorkbook workbook1 = new HSSFWorkbook();
    //long task...
    ServletOutputStream outputStream = response.getOutputStream();
    workbook1.write(outputStream);
    workbook1.close();
}


@RequestMapping(value = "/b", method = RequestMethod.GET, produces = APPLICATION_EXCEL)
public @ResponseBody
void downloadB(HttpServletResponse response) throws IOException {
    response.setContentType(APPLICATION_EXCEL);
    response.setHeader("Content-Disposition", "attachment; filename=" + "b.xlsx");
    HSSFWorkbook workbook1 = new HSSFWorkbook();
    ServletOutputStream outputStream = response.getOutputStream();
    workbook1.write(outputStream);
    workbook1.close();
}
diddles
  • 11
  • 5
  • Welcome to Stack Overflow. Please review [ask] and [mvce]. That's not how requests/response work in Spring. Each request/response will have its own input and output. – Jason Armstrong Dec 12 '18 at 16:18
  • Thanks for your fast reponse. I uploaded some sample code and still confused why a does not write out to output stream if they each have their own response. – diddles Dec 12 '18 at 23:32
  • This is also from the same user/session – diddles Dec 12 '18 at 23:47
  • What is the error you are getting? Can you update the question with the error and any stack traces that occur? – Jason Armstrong Dec 13 '18 at 00:28
  • With the sample above, I do not get an IO exception but file a never gets downloaded. It looks like the second requests overwrites the first. – diddles Dec 13 '18 at 00:42
  • One thing to try is removing the close() method call see [this](https://stackoverflow.com/questions/1829784/should-i-close-the-servlet-outputstream) for details. – Jason Armstrong Dec 13 '18 at 00:54
  • Technically, that should not matter because each request has its own io? – diddles Dec 13 '18 at 05:55
  • I posted the error, which was originally being squashed. So it looks like it maybe on the client side closing the socket? – diddles Dec 13 '18 at 15:19
  • I used the resource monitor tool from Windows and saw the second request closes the tcp connection. – diddles Dec 13 '18 at 16:10
  • Ok, that’s good information coupled with the session. It seems that there may be something going on in the client code that is not handling the session correctly or something is happening deep in the long running task that affects the session (less likely). – Jason Armstrong Dec 13 '18 at 16:16
  • The other reason for not closing the stream yourself is if you have any response filters enabled. If the stream is closed and they try to write that’ll fail. You should see an exception in that case. – Jason Armstrong Dec 13 '18 at 16:17
  • This is the exception I am getting "java.net.SocketException: Software caused connection abort: socket write error" – diddles Dec 13 '18 at 16:43
  • So i fixed the issue by setting the href to target="_blank", this creates a new tab for the request but I still dont understand why I cant use the same tab. – diddles Dec 13 '18 at 17:46
  • Works if I embed it into an iframe without the need of an open tab. I am curious if this is how you can do multiple downloads in the same window – diddles Dec 13 '18 at 18:31

0 Answers0