on 2018 Dec 04 3:55 PM
I have media file with data. How could I create controller that will return this media in csv format for example?
Request clarification before answering.
We used a controller to download PDF , could be similar for CSV
@ResponseBody
@RequestMapping(value = { "/something" }, method = { RequestMethod.GET })
public void someMethod(@PathVariable String code, HttpServletRequest request, HttpServletResponse response) {
try {
byte[] pdfData = someFacade.getPDF(code);
if (StringUtils.isNotEmpty(code)) {
response.setHeader("Content-Disposition", new StringBuilder().append("inline;filename=").append(code).append("Document").toString());
}
response.setDateHeader("Expires", -1L);
response.setContentType("application/pdf");
response.getOutputStream().write(pdfData);
response.getOutputStream().close();
response.getOutputStream().flush();
} catch (UnknownIdentifierException | IOException exception) {
LOG.error("exception", exception);
response.setStatus(500);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi , there are a few things to be taken care of here.
First, never return your Model from the controller, instead use a converter to convert MediaModel into MediaData and return MediaData from this controller
Second, you need to set "text/csv" on the response object so that your browser will know that you are returning CSV.
So your controller should look like below
@RequestMapping(value = "/getlastreview", method = RequestMethod.GET) @ResponseBody public MediaData getData (HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/csv");
MediaModel mediaModel = dataFacade.getLastReview();
MediaData mediaData = mediaModelConverter.convert(mediaModel);
// convert mediaData into CSV using supercsv
}
use this link to understand how to use supercsv API https://www.codejava.net/frameworks/spring/spring-mvc-with-csv-file-download-example
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.