Showing posts with label Web Info. Show all posts
Showing posts with label Web Info. Show all posts

Sunday, August 14, 2011

show Image from database in jsp


We can provide a servlet as the source of the image. ans in the servlet we need to some little cook. This code may help you in the regards.

/*
* This Method Returns the right MIME type for a particular format
*


* @param String format ex: xml or HTML etc.
* @return String MIMEtype
*/
private String getMimeType(String format)
{
if(format.equalsIgnoreCase("pdf")) //check the out type
return "application/pdf";
else if(format.equalsIgnoreCase("audio_basic"))
return "audio/basic";
else if(format.equalsIgnoreCase("audio_wav"))
return "audio/wav";
else if(format.equalsIgnoreCase("image_gif"))
return "image/gif";
else if(format.equalsIgnoreCase("image_jpeg"))
return "image/jpeg";
else if(format.equalsIgnoreCase("image_bmp"))
return "image/bmp";
else if(format.equalsIgnoreCase("image_x-png"))
return "image/x-png";
else if(format.equalsIgnoreCase("msdownload"))
return "application/x-msdownload";
else if(format.equalsIgnoreCase("video_avi"))
return "video/avi";
else if(format.equalsIgnoreCase("video_mpeg"))
return "video/mpeg";
else if(format.equalsIgnoreCase("html"))
return "text/html";
else if(format.equalsIgnoreCase("xml"))
return "text/xml";
else
return null;
}

Step Two
Get the reference to the right OutPutStream. Use ServletOutPutStream, where as for character data you'd use PrintWriter, the java.io class that prints objects to a text-output stream. This following snippet is for binary data:


ServletOutputStream sOutStream = response.getOutputStream();

Step Three
Create BufferedInputStream from the InputStream:


BufferedInputStream bis = null;
InputStream in = urlc.getInputStream();
bis = new BufferedInputStream(in);




Step Four
Create BufferedOutPutStream with a new ServletOutPutStream to which you can write:


BufferedOutputStream bos = null;
bos = new BufferedOutputStream(sOutStream);

Step Five
Read in to the bytes array from BufferedInputStream:


byte[] buff = new byte[length];
int bytesRead;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}

Step Six
Write on to BufferedOutPutStream from the bytes array, which in turn streams to the client. Use the streamBinaryData() method for binary data streaming:


/*
* This Method Handles streaming Binary data
*


* @param String urlstr ex: http;//localhost/test.pdf etc.
* @param String format ex: pdf or audio_wav or msdocuments etc.
* @param ServletOutputStream outstr
* @param HttpServletResponse resp
*/
private void streamBinaryData(String urlstr,String format,
ServletOutputStream outstr, HttpServletResponse resp)
{
String ErrorStr = null;
try{
//find the right MIME type and set it as contenttype
resp.setContentType(getMimeType(format));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
URL url = new URL(urlstr);
URLConnection urlc= url.openConnection();
int length = urlc.getContentLength();
resp.setContentLength(length);
// Use Buffered Stream for reading/writing.
InputStream in = urlc.getInputStream();
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(outstr);
byte[] buff = new byte[length];
int bytesRead;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
ErrorStr = "Error Streaming the Data";
outstr.print(ErrorStr);
} finally {
if( bis != null ) {
bis.close();
}
if( bos != null ) {
bos.close();
}
if( outstr != null ) {
outstr.flush();
outstr.close();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}

N.B In the up coming post, i will try to note a complete sequence to show image from database.

Sunday, January 9, 2011

Gmail Modes

Many times, we are not able to access Gmail account from our PC for various reason. And that can not be performed for various issues. We can follow the modes depicted below for getting help on requirements.

Gmail Modes


If you can't access Gmail, try some of these URLs:

Safe mode - http://mail.google.com/mail/?labs=0. It disables the experimental features from Gmail Labs, just in case some of them are buggy. You can remove some of the features from Gmail's settings page.

Secure mode - https://mail.google.com/. It encrypts the traffic between your computer and Gmail's servers. Use it from public computers, Wi-Fi networks or to bypass some proxies and web accelerators. There's a Gmail setting that redirects the standard version to the secure mode ("Always use https").

Older version - http://mail.google.com/mail/?ui=1. This version has been replaced in October 2007 by a rearchitectured Gmail, but the old version is a little bit faster.

Basic mode - http://mail.google.com/mail/?ui=html. It's the version that doesn't use JavaScript, so it loads faster and it works well with older browsers. Unfortunately, many Gmail features are missing (contacts autocomplete, chat, spell checker, rich formatting) and each click loads a new page. If you like this version, click on "Set basic HTML as default view" at the top of the page.

Mobile mode - http://mail.google.com/mail/?ui=mobile or http://m.gmail.com. This is a simplified Gmail interface for mobile phones that has even less feature than the basic mode. Use it if no other Gmail mode works for you.

iPhone mode - http://mail.google.com/mail/x/gdlakb-/gp/. A more user-friendly mobile version for iPhone and other mobile phones that use WebKit-based browsers.

iGoogle gadget - http://www.google.com/ig/gmailmax. This is the canvas view for the updated Gmail gadget which can be found in the new iGoogle. Some people found that this interface bypasses most corporate filters that prevent them from accessing Gmail at work.

"No browser checking" mode - http://mail.google.com/mail?nocheckbrowser. If you use a cutting-edge new browser and Gmail serves you the basic HTML mode, try this URL to bypass browser detection.