Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring a (possibly large) file between requests in Spring
    text
    copied!<p>I have this controller methods that depending on the parameters introduced by the user downloads a certain PDF file and shows a view with its different pages converted to PNG.</p> <p>So the way I approached it works like this:</p> <p>First I map a method to receive the post data sent by the user, then generate the URL of the actual PDF converter and pass it to the model:</p> <pre><code>@RequestMapping(method = RequestMethod.POST) public String formPost(Model model, HttpServletRequest request) { //Gather parameters and generate PDF url Long idPdf = Long.parseLong(request.getParam("idPdf")); //feed the jsp the url of the to-be-generated image model.addAttribute("image", "getImage?idPdf=" + idPdf); } </code></pre> <p>Then in getImageMethod I download the PDF and then generate a PNG out of it:</p> <pre><code>@RequestMapping("/getImage") public HttpEntity&lt;byte[]&gt; getPdfToImage(@RequestParam Long idPdf) { String url = "myPDFrepository?idPDF=" + idPdf; URL urlUrl = new URL(url); URLConnection urlConnection; urlConnection = urlUrl.openConnection(); InputStream is = urlConnection.getInputStream(); return PDFtoPNGConverter.convert(is); } </code></pre> <p>My JSP just has an img tag that refers to this url:</p> <pre><code>&lt;img src="${image}" /&gt; </code></pre> <p>So far this work perfectly. But now I need to allow the possibility of viewing multi page PDFs, converted as PNGS, each of them in a different page. So I would add a <code>page</code> parameter, then feed my model with the image url including that page parameter, and in my <code>getImage</code> method I would convert only that page. </p> <p>But the way it is implemented, I would be downloading the PDF again for each page, plus an additional time for the view, so it can find out whether this specific PDF has more pages and then show the "prev" and "next" buttons.</p> <p>What would be a good way to preserve the same file during these requests, so I download it just once? I thought about using temp files but then managing its deletion might be a problem. So maybe storing the PDF in the session would be a good solution? I don't even know if this is good practice or not.</p> <p>I am using Spring MVC by the way.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload