Struts 2 - parametros result type

Pesssoal ,
preciso fazer uma tela que faça download , mas não estou entedendo como funicionam os parametros do resultType=stream, vi na doc do site mas não ajuda muito :
http://struts.apache.org/2.x/docs/stream-result.html

<result name="success" type="stream">
  <param name="contentType">image/jpeg</param>
  <param name="inputName">imageStream</param>
  <param name="contentDisposition">filename="document.pdf"</param>
  <param name="bufferSize">1024</param>
</result> 

o que deve conter a minha action ? como colocar parametros dinamicos ?

Valeu !

Herrera

Cara, tenta isso…

public class MyExampleFileDownload extends DownloadAction{


protected StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// File Name
String fileName = mapping.getParameter();

// Set the content disposition
response.setHeader("Content-disposition",
"attachment; filename=" + fileName);

// Download a "pdf" file - gets the file name from the
// Action Mapping's parameter
String contentType = "application/pdf";
File file = new File(fileName);

return new FileStreamInfo(contentType, file);

}
}

Segue tambem uma referencia pra vc ter como se virar
http://wiki.apache.org/struts/StrutsFileDownload

valeu pela dica, mas estou usando Struts 2.

abçs

Herrera

Herrera,

Da uma olhada na api e no codigo do struts2, que é bem melhor que o tutorial: http://struts.apache.org/2.0.8/struts2-core/apidocs/org/apache/struts2/dispatcher/StreamResult.html

Mas basicamente,

Poe no teu config:

    <action name="mydownload" class="DownloadAction">
      <result name="mystream" type="stream">
	      <param name="contentType">${myContentType}</param>
	      <param name="contentLength">${myContentLength}</param>
	      <param name="contentDisposition">${myContentDisposition}</param>
	      <param name="inputName">myInputStream</param>
      </result>
    </action

Comments: os ${something} sao resolvidos no valuestack do struts config (na pratica se vc esta utilizando a fullstack sao os getters do teu action)

poe na tua action:

public class DownloadAction {
    
    private Myfile myfile; 
    private Long id;

    // ----- Simple Getters / Setters
    
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    // ----- StreamResult Stuff
    
    public String getMyContentType() {
    	return myfile.getFileType();
    }
    public Integer getMyContentLength() {
    	return myfyle.getFileSize();
    }
    public String getMyContentDisposition() {
    	return "filename=\"" + myfile.getFileName() +"\"";
    }
    public InputStream getMyInputStream() {
    		return new ByteArrayInputStream(myfile.getArchive());
    }
    
    // ----- Myfile stuff
    
    public Myfile getMyfile() {
        return myfile;
    }
    public void setMyfile(Myfile user) {
        this.myfile = myfile;
    }

    //********************************* Control Methods

    public String execute() {
        setMyfile(DAO.findMyFile(this.id));
    	return "mystream";
    }

}

Comments 1: MyFile é um javabean com os atributos:

  • String fileName;
  • String fileType;
  • Integer fileSize;
  • byte[] archive;
    Comments 2: DAO.findMyFile é uma implementacao simples de busca do MyFile com ‘id’ em um banco de dados.
    Comments 3: pra fazer o download funcionar precisa informar http://{myserver}/{mycontext}/mydownload?id={myid}

Espero ter ajudado,

Glauber