Implemented using Spring MVC, rest service.
Display file on screen or download as attachment.
@Controller
public class FileController
{
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@RequestMapping(value = "/file/{id}", method = RequestMethod.GET)
public void getFileViaAttachment(@PathVariable String id, HttpServletResponse response)
{
log.info("getFile " + id);
try
{
String content = "testing file download";
String fileName = "mojFileIzStringa.txt";
InputStream in = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
IOUtils.copy(in, response.getOutputStream());
response.setContentType("application/text");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.flushBuffer();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@RequestMapping(value = "/many", method = RequestMethod.GET)
public void getManyLinesViaAttachment(HttpServletResponse response)
{
log.info("getManyLines ");
try
{
String[] linesArrays = { "prva linija", "druga linija", "xxx linija" };
List<String> linesList = Arrays.asList(linesArrays);
String fileName = "mojFileSpunoLinija.csv";
IOUtils.writeLines(linesList, null, response.getOutputStream(), StandardCharsets.UTF_8);
response.setContentType("application/text");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.flushBuffer();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// every line in new row
@RequestMapping(value = "/getFileAsItIs", method = RequestMethod.GET, produces = "text/plain")
@ResponseBody
public String getFileAsItIs()
{
log.info("getFileAsItIs ");
String[] linesArrays = { "prva linija", "druga linija", "xxx linija" };
List<String> linesList = Arrays.asList(linesArrays);
StringBuilder sb = new StringBuilder();
for (String s : linesList)
{
sb.append(s).append(System.lineSeparator());
}
return sb.toString();
}
}
Showing posts with label servlet. Show all posts
Showing posts with label servlet. Show all posts
Monday, March 23, 2015
Wednesday, June 1, 2011
Export to pdf
There is datatable and when user click on particular row it needs to be printed.
data table in xhtml:
<ice:dataTable value="#{activeBackingBean.selectedEntity.subjectableList}" var="bill" ...
inside column put link component instead outputTex, which will pass bill number:
<ice:commandLink value="#{bill.GUINumber}" actionListener="#{activeBackingBean.billToPdf}" style="color:black;" title="Print bill">
<f:param name="billNumber" value="#{bill.GUINumber}" />
</ice:commandLink>
In backing beanu there is (actionListener) method:
public void billToPdf(ActionEvent ae)
{
read request parametar and save object in class field for later use, call hidden button
String parameter = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("billNumber");
log.debug("billToPdf: " + parameter);
//find appropriate object (bill)
JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "document.getElementById('PrintForm:PrintButton').click();");
}
hidden button in xhtml:
<ice:form id="PrintForm">
<ice:commandButton id="PrintButton" visible="false" actionListener="#"
onclick="{location.href='#{downloadServlet}?fileCreator=null&fileType=pdf&fileAction=activeBackingBean.billToPdf'}" />
</ice:form>
Click will call servlet which will call appropriate method in bean via reflection (fileAction -our method which needs to be called):
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
ctx.getBean("activeBackingBean");
Object result = MethodUtils.invokeMethod(bean, methodName, iS); //iS is InputStream
ServletOutputStream stream = response.getOutputStream(); ...
in backing beanu method that exports to PDF:
public FileObject billToPdf() throws JRException, IOException
{
if (log.isDebugEnabled()) log.debug("monthBillToPdf: " + bill);
return billManager.billToPdf(bill);
}
Second approach:
call servlet with submitted oid. Via refection call bean method, fetch appropriate object and print it.
data table in xhtml:
<ice:dataTable value="#{activeBackingBean.selectedEntity.subjectableList}" var="bill" ...
inside column put link component instead outputTex, which will pass bill number:
<ice:commandLink value="#{bill.GUINumber}" actionListener="#{activeBackingBean.billToPdf}" style="color:black;" title="Print bill">
<f:param name="billNumber" value="#{bill.GUINumber}" />
</ice:commandLink>
In backing beanu there is (actionListener) method:
public void billToPdf(ActionEvent ae)
{
read request parametar and save object in class field for later use, call hidden button
String parameter = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("billNumber");
log.debug("billToPdf: " + parameter);
//find appropriate object (bill)
JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "document.getElementById('PrintForm:PrintButton').click();");
}
hidden button in xhtml:
<ice:form id="PrintForm">
<ice:commandButton id="PrintButton" visible="false" actionListener="#"
onclick="{location.href='#{downloadServlet}?fileCreator=null&fileType=pdf&fileAction=activeBackingBean.billToPdf'}" />
</ice:form>
Click will call servlet which will call appropriate method in bean via reflection (fileAction -our method which needs to be called):
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
ctx.getBean("activeBackingBean");
Object result = MethodUtils.invokeMethod(bean, methodName, iS); //iS is InputStream
ServletOutputStream stream = response.getOutputStream(); ...
in backing beanu method that exports to PDF:
public FileObject billToPdf() throws JRException, IOException
{
if (log.isDebugEnabled()) log.debug("monthBillToPdf: " + bill);
return billManager.billToPdf(bill);
}
Second approach:
call servlet with submitted oid. Via refection call bean method, fetch appropriate object and print it.
Subscribe to:
Posts (Atom)