stream video from struts2 action multiple contentType?

前端 未结 1 430
轮回少年
轮回少年 2020-12-12 04:07

I am trying to play video file, for my action contentType is set to

application/octet-stream

now if i change it to audio/mpeg, t

相关标签:
1条回答
  • 2020-12-12 04:20

    Of course you can.

    You must output the Stream Result type from your Action, and specify a parametric contentType, for example:

    Struts.xml

    <result name="success" type="stream">
      <param name="contentType">${yourContentType}</param>
      <param name="inputName">inputStream</param>
      <param name="contentDisposition">attachment;filename="${yourFileName}"</param>
      <param name="bufferSize">1024</param>
    </result>
    

    Action

    @Getter @Setter private InputStream inputStream;
    @Getter private String yourContentType;
    @Getter private String yourFileName;
    
    public String execute() throws Exception {
    
       yourContentType = "audio/mpeg";
       yourFileName = "yourStuff.mp3";
       byte[] yourContent = loadTheContentInSomeWay();
    
       setInputStream(new ByteArrayInputStream(yourContent));        
    
       return SUCCESS;
    }
    

    You can parameterize the contentDisposition part to specify when a file must be opened as attachment (ask for download) or inline (open in browser) according to your needs.

    0 讨论(0)
提交回复
热议问题