When I try to view a video in the jwplayer from a file in the webcontent directory it shows up and I can play it, but when I read the same file from a database and respond with an flv via servlet it doesn't show up. Can any one help me?
In Html file :
<script type='text/javascript' src='/ThoughRecord18-8/jwplayer.js'></script>
<script type='text/javascript'>
jwplayer('mediaspace').setup({
'flashplayer': '/ThoughRecord18-8/player.swf',
'file': '/ThoughRecord18-8/videoss?videoId=1',
'controlbar': 'bottom',
'width': '470',
'height': '320'
});
</script>
and the servlet is
String videoId = request.getParameter("videoId");
if (videoId != null || !videoId.equals("")) {
VideoDao dao = new VideoDao();
Video video = dao.getVideo(videoId);
Blob blob = video.getVideoBlob();
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in = null;
int len;
try {
len = (int) blob.length();
byte[] rb = new byte[len];
InputStream readImg = blob.getBinaryStream();
int index = readImg.read(rb, 0, len);
...
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("video/x-flv");
response.setContentLength(rb.length);
response.setHeader("Content-Disposition", "inline; filename=file.flv");
byte[] content = new byte[DEFAULT_BUFFER_SIZE];
BufferedInputStream is = new BufferedInputStream(
new ByteArrayInputStream(rb));
OutputStream os = response.getOutputStream();
while (is.read(content) != -1) {
os.write(content);
}
is.close();
os.close();**
This isn't a java issue, JW Player only supports HTTP Psuedo Streaming and RTMP Streaming. They're both their own protocols - you can't just stream the pure content at it. Take a look at this page: http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12534/video-delivery-http-pseudo-streaming, and this page: http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12535/video-delivery-rtmp-streaming for info on how JW Player does streaming.
If you don't want the user to have to wait to get all of the content, you'll need to go with one of those streaming mechanisms. If that's not an issue, you could consider changing your servlet to write the file somewhere in your webcontent directory and then do a redirect to the file or something, but I don't think writing to the response stream like that is going to do the trick.
Related
I am trying to write in an existing pdf file and want to open it in new browser at clients machine. I saw many post related this and i tried all answers but still i am not getting result. File can be generated on my disk but i want to open it at new window once created.
Code :
response.setContentType("application/pdf");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream("Challan.pdf");
PdfReader pdfReader = new PdfReader(is, null);
PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);
for(int i=1; i<= pdfReader.getNumberOfPages(); i++){
Phrase Namephrase = new Phrase("John Smith");
Phrase Idphrase = new Phrase("DPG10001");
Phrase Datephrase = new Phrase("11/10/1980");
PdfContentByte pdfcontent = pdfStamper.getUnderContent(i);
pdfcontent.beginText();
ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED_ALL, Idphrase, 500, 428, 0); /*Do not change this values*/
ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED, Namephrase, 500, 407, 0);
ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED, Datephrase, 500, 386, 0);
pdfcontent.endText();
}
pdfStamper.close();
pdfReader.close();
// write ByteArrayOutputStream to the ServletOutputStream
response.setHeader("Content-Disposition", "inline;filename=ChallanStamped.pdf");
// the contentlength
response.setContentLength(baos.size());
ServletOutputStream sos = response.getOutputStream();
baos.writeTo(sos);
sos.flush();
sos.close();
In this i am trying to read challan.pdf and want to update file and create new one.From jsp i am calling this servlet.
Please help me.
Update:
I tried with some changes but still not working. My updated code is
` ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedInputStream is = new BufferedInputStream(new FileInputStream("Challan.pdf"));
PdfReader pdfReader = new PdfReader(is, null);
PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);
-----------Some Code -------------
pdfStamper.close();
pdfReader.close();
response.setHeader("Content-Disposition", "inline;filename=ChallanStamped.pdf");
response.setContentLength(baos.size());
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[4096];
int length;
while ((length = input.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
I am calling servlet from jsp through Ajax(sending lot of data).
And also i am getting java.io.IOException: Stream closed exception with this updated code.
In order to open the pdf file in new window you need to do some changes in your jsp form.
<form action="yourServletURLPattern" method="post" target="_blank">
-- Some Fields--
-- Submit Button--
</form>
target=_blank will do the needful.
Now coming to your servlet code :
ByteArrayOutputStream baos = new ByteArrayOutputStream();
---------- Some Code in between-----------------------
ServletOutputStream sos = response.getOutputStream();
baos.writeTo(sos);
But this do nothing since your ByteArrayOutputStream Object contains nothing so what will you write to ServletOutputStream
I will not write code for you but can guide, So that you can achieve it yourself
Read File after editing using BufferedInputStream
Write it on OutputStream of Response using BufferedOutputStream (Simply Loop
through the file)
Flush the Response's Output Stream
I Want to print html as a pdf. Here is my code. It was not throwing any error but when i open pdf it is showing error
Here is my java code
URL u = new URL("http://localhost/printPdf.Html");
URLConnection uc = u.openConnection();
BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
File outs = new File("D:/HtmlToPdf.pdf")
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outs));
byte[] b = new byte[8 * 1024];
int read = 0;
while ((read = is.read(b)) > -1) {
bout.write(b, 0, read);
}
bout.flush();
bout.close();
is.close();
Here is my html file
<html>
<head></head>
<body><div>Hi !!! Example PDF </div></body>
</html>
When i open pdf i am getting this error
Adobe Reader could not open because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
You cannot simply write one file format to another. You need to write the HTML in a way according to the PDF specification.
Here is a PDF library you can use: http://pdfbox.apache.org/
I'm facing performance issue while opening pdf files in browser.It is taking very huge to time to open.Our pdf files are scanned images. So is there any alternative solution for this. Here is my sample code.
response.setContentType("application/pdf");
response.setContentLength((int) file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
Here i'm rendering outstream to browser.
I am working with JasperReprots. This is part of my code:
ServletContext context = this.getServletConfig().getServletContext();
File reportF = new File(context.getRealPath(rF));
byte[] bytes = null;
ServletOutputStream servletOutputStream = resp.getOutputStream();
InputStream reportStream = new FileInputStream(reportF.getPath());
reportF.delete();
bytes = JasperRunManager.runReportToPdf(reportStream, new HashMap(),new JREmptyDataSource());
resp.setContentType("application/pdf");
resp.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
After this I can see pdf in my browser, but when I try to save it, the file has no extension pdf. How to add this extension without saving report on my server?
This should probably do the trick:
resp.setHeader("Content-Disposition", "attachment;filename=report.pdf");
I have opened a webpage in HtmlUnit headless browser. Now that webpage contains a image html tag as follows:
<img src="..." />
So I want that image only. But the problem is that the same src URL of the image shows diff. image each time. Means, if we refresh the img src URL, then it shows diff. image each time.
So how to get the image that is displayed on the html page.
When you get the HTMLPage, you have to get the image through one of its method. You can then get an HtmlImage, which can be saved as a file. You'll just have to analyse this file later.
This is the function to store your image with fully qualified I
protected String saveImage(String imageUrl) throws Exception {
InputStream inputStream;
OutputStream os;
ByteArrayOutputStream byteArrayOutputStream;
String destinationFile = "File path where you want ot store the image";
URL url = new URL(imageUrl);
inputStream = url.openStream();
byteArrayOutputStream = new ByteArrayOutputStream();
os = new FileOutputStream(destinationFile);
int read;
String barcode = null;
while ((read = inputStream.read()) != -1) {
os.write(read);
byteArrayOutputStream.write(read);
barcode = byteArrayOutputStream.toString();
}
inputStream.close();
os.close();
byteArrayOutputStream.close();
return barcode;
}