File between backend a front end (full example)
You can create a method in wich your return a file as a stream , the you assign the url of this last to your link button ,
@RequestMapping(value="getFile", method=RequestMethod.GET)public void getFile(HttpServletResponse response,HttpServletRequest request) { String environmentName = request.getParameter("name"); //here the code to get your file as stream //whether getting the file by Ressource or buffred , //here for example I named a getfileStream() method wihch return your file InputStream InputStream myStream = getFileStream(environmentName); // Set the content type and attachment header add filename and it's extention. response.addHeader("Content-disposition", "attachment;filename=myfile.myExtention"); response.setContentType("txt/plain"); // copy your file stream to Response IOUtils.copy(myStream, response.getOutputStream()); response.flushBuffer();}
in order to get the name parameter
you just pass it to the modelview
in the /report
controller , an then assign it to your link .
As below :
@RequestMapping("/report")public ModelAndView report(HttpServletRequest request) { String environmentName = request.getParameter("name"); ModelAndView model = new ModelAndView("report"); model.addObject("name", environmentName); return model;}
then your link would be like :
<a href="<c:url value="/getFile" />?name=${name}">Get file</a>
the getFileStream could be like :
public InputStream getFileStream(String environmentName) { Container container = getContainerID(environmentName); String url = ""; if(container != null) { Settings settings = Settings.getSettings(); url = "http://" + settings.getDockerIP() + ":" + settings.getDockerPort() + "/containers/" + container.getId() + "/archive?path=/path/file"; } InputStream is = new URL(url).openStream(); return is; }
You have to add the following appace common io to your project in order to use the IOUtils
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version></dependency>