您可以创建一个方法,将文件作为流返回,将最后一个URL分配给链接按钮,
@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();
}
为了获取name参数,您只需将其传递给/ report控制器中的modelview,然后将其分配给您的链接.
如下 :
@RequestMapping("/report")
public ModelAndView report(HttpServletRequest request) {
String environmentName = request.getParameter("name");
ModelAndView model = new ModelAndView("report");
model.addObject("name", environmentName);
return model;
}
然后你的链接将是:
getFileStream可能是这样的:
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;
}
您必须在项目中添加以下appace common io才能使用IOUtils
commons-io
commons-io
2.5