Hi,i need to display image in a Box like in profile photo,when i click on the browse button,file gets open and the selected image should diplay in a box,no need to save it in database because i hav the code to save it in a database....Kindly post JSP codes..Its Urgent, I m very thankful t those who solve ma problem..
with regards, GKK
Sorry i m not asked like this..... for example in facebook we have profile photo upload,there the selected image will be displayed in the same page..like that i need a jsp code, the actual requirment is this... image tag(we hav given size) Browse button(belove the image tag)
once we click the browse button the selected image should uploaded to the image tag... and once we click the save button the image should be saved in the database for future use.. Please helm me...I need JSP code for this...
The given code allow the user to select any file. This file is then uploaded and displayed on the browser.
1)page.jsp:
<%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> <BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST> <br><br><br> <center> <table border="0" bgcolor=#ccFDDEE> <tr> <center> <td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td><b>Choose the file To Upload:</b></td> <td><INPUT NAME="file" TYPE="file"></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Send File"> </td> </tr> <table> </center> </FORM> </BODY> </HTML>
2)upload.jsp:
<%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%! private byte[] getImage(String filename) { byte[] result = null; String fileLocation = filename; File f = new File(fileLocation); result = new byte[(int)f.length()]; try { FileInputStream in = new FileInputStream(fileLocation); in.read(result); } catch(Exception ex) { System.out.println("GET IMAGE PROBLEM :: "+ex); ex.printStackTrace(); } return result; } %> <% String saveFile=""; String contentType = request.getContentType(); if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){ DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while(totalBytesRead < formDataLength){ byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; File ff = new File(saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); response.reset(); response.setHeader("Content-Disposition", "inline"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Expires", "0"); response.setContentType("image/jpg"); byte[] image = getImage(saveFile); OutputStream outputStream = response.getOutputStream(); outputStream.write(image); outputStream.close(); } %>
Ads