How do I implement a controller class that calls from an external application that is not Java and returns download files?
You can access the following URL to open a download dialog on the caller screen:
If Spring does not return jsp and only returns the download file, the return will be empty if implemented below.
What is the correct implementation method?
@Controller
public class downloadController {
@RequestMapping(path="/download", method="GET")
public String download (@RequestParamparam, HttpServletResponseres) arrows Exception {
// processing of something
res.setContentType("application/octed-stream");
res.Header("Content-Disposition", String.format(" attachment; filename=\"+fileName+"\""));
FileCopyUtils.copy(new BufferedInputStream(newFileInputStream(file))), res.getOutputStream());
return '';
}
}
configuring:dynamic projects
Spring: 5.3.19
Implement the View
(AbstractView
) class that returns the download file, and implement the Controller
class to return the View name.(Image to move what Controller
implements to View
)
Implementation Image (View)
import org.springframework.web.servlet.view.AbstractView;
// Omit other import
@Component
public class DownloadView extensions AbstractView {
@ Override
protected void renderMergedOutputModel(
Map<String, Object>model,
HttpServletRequest request,
HttpServletResponse response)
US>throws Exception {
String fileName=(String)model.get("fileName");
response.setContentType("application/octed-stream");
response.Header("Content-Disposition", String.format(" attachment; filename=\"+fileName+"\");
FileCopyUtils.copy(new BufferedInputStream(newFileInputStream(filename))), response.getOutputStream());
}
}
Implementation Image (Controller)
@Controller
public class downloadController {
@RequestMapping(path="/download", method="GET")
public String download (@RequestParamparam, HttpServletResponseres, Model model) arrows Exception {
// processing of something
// set the filename to download
model.addAttribute("fileName", param.getFilename());
return "downloadView";
}
}
© 2024 OneMinuteCode. All rights reserved.