Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

java - How to define the stream Result annotation?

I need to export the Excel sheet, so calling action class method through Ajax call from dialog window. I have Excel icon, when I click the icon calling method and processing the backend data, and finally going to execute the below code and return the result to download the Excel sheet.

jQuery Ajax calling:

function callajax() {
     jQuery.ajax({
     url : '<s:url action="part" method="export"/>',

Result method:

@Result(name="success",type=StreamResult.class,value="",params={"inputName","inputStream"})

Java code:

    public String method {
    -----------------
    method to call backend...
    --------------------------
    httpServletResponse.setContentType("application/vnd.ms-excel");
    String filename = filters.getPeriod() + "_" +filters.getRegion() ;
    httpServletResponse.setHeader("Content-disposition",
    "attachment; filename="+filename+".xls");
    ServletOutputStream outputStream = httpServletResponse.getOutputStream();
    builder.build(method1, method2, outputStream);
    outputStream.flush();
    return SUCCESS; }

In the build method has creating workbook

    WorkbookSettings workbookSettings = new WorkbookSettings();
    workbookSettings.setLocale(new Locale("en", "EN"));
    WritableWorkbook workbook =
    Workbook.createWorkbook(outputStream, workbookSettings);

and setting the all the values in sheet. Please advise.

Trying as your advise :

I'm using code behind , so result type on class name :

@Result(name="success",type=StreamResult.class,value="",params = {"contentType","application/octet-stream","inputName","inputStream","bufferSize","1024","contentDisposition","attachment;filename="${filename}""})


public String method {
    -----------------
    method to call backend...
    --------------------------
String filename =   filters.getRegion() + "_" +
                filters.getCurrency();
File file = new File(filename);
inputStream = new FileInputStream(file); //getting error "java.io.FileNotFoundException:file "
builder.buildXL(OBJ1, OBJ2, file);
return SUCCESS;
}

buildxl(obj1,obj2,File file)
{
WorkbookSettings workbookSettings = new WorkbookSettings();
    workbookSettings.setLocale(new Locale("en", "EN"));
    WritableWorkbook workbook =
    Workbook.createWorkbook(file, workbookSettings);

-----
------
workbook.write(); 
workbook.close();
}

Params values :

params = 
{"contentType","application/octet-stream",
"inputName","inputStream",
"bufferSize","1024",
"contentDisposition","attachment;filename="${filename}""})

Log :

2013-07-18 10:25:33,453 ERROR         org.apache.struts2.dispatcher.StreamResult - Can not find a java.io.InputStream with the name [] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
2013-07-18 10:25:33,453 ERROR       org.apache.struts2.rest.RestActionInvocation - Exception processing the result.
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
    at org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
    at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    at org.apache.struts2.rest.RestActionInvocation.executeResult(RestActionInvocation.java:241)
    at org.apache.struts2.rest.RestActionInvocation.processResult(RestActionInvocation.java:198)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:146)
    at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:147)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
    at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3288)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1513)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
2013-07-18 10:25:33,499 DEBUG       org.apache.struts2.rest.RestActionInvocation - No result returned for action at null

ClasspathPackageProvider:

org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[contentType:application/octet-stream] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[inputName:inputStream] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[bufferSize:1024] to result.
org.apache.struts2.config.ClasspathPackageProvider - Adding parmeter[contentDisposition:attachment;filename="${filename}"] to result.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Return result as a binary stream

private InputStream inputStream;
private String filename; 

//getters here
public InputStream getInputStream() {
    return inputStream;
}

public String getFilename() {
  return filename;
}

@Result(name="success", type=StreamResult.class, value="inputStream", params={"contentType","application/octet-stream", "contentDisposition", "attachment;filename="${filename}"", "inputName", "inputStream"})

String filename = filters.getPeriod() + "_" +filters.getRegion() ;
File file = new File(filename);
inputStream = new FileInputStream(file);
return SUCCESS;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...