1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Aadam
  • 1,521
  • 9
  • 30
  • 60

1 Answers1

2

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.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • byte[] yourContent = loadTheContentInSomeWay(); i didn't understand – srinivas gowda May 05 '17 at 06:25
  • this is working when i put video (Sample.mp4) under web content in eclipse but not system location – srinivas gowda May 05 '17 at 06:27
  • @srinivasgowda You need a way to load your byte[] from the source you have: from a File, from a database, from a Service, from a Queue, ecc.... Since it is not relevant to this answer, I've wrapped it in a fake method called `loadTheContentInSomeWay();`. Pretty much self-explanatory, I hope(d) :) – Andrea Ligios May 05 '17 at 07:38
  • can you see this question tell where i need to correct https://stackoverflow.com/questions/44126896/how-do-i-play-multiple-videos-in-jsp-using-struts2 – srinivas gowda May 23 '17 at 08:10