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
167 views
in Technique[技术] by (71.8m points)

java - stream video from struts2 action multiple contentType?

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, then user cant download other types of files. I would like to simply know can we set multiple content type if so how? and if its not possible what should i do in a situation where user can upload and download anytype of file.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.


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

...