hi!
In my previous interview i got two questions which i could not answer regarding file upload and annotations. I want to know which is the best method can be used for file upload. Whether moving them into virtual path in the server or converting them into bytes and stored into the database.
What are annotations and application areas of annotations....
Please let me know about the above questions....
Thanks, Regards, VASU.
Hello Friend,
You can use both the methods in a single application.
Try the following code: 1)page.jsp:
<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.*,java.sql.*" %> <% 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; saveFile="C:/UploadedFiles/"+saveFile; File ff = new File(saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b> <% out.println(saveFile);%></td></tr></table> <% Connection connection = null; String connectionURL = "jdbc:mysql://localhost:3306/test"; ResultSet rs = null; PreparedStatement psmnt = null; FileInputStream fis; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "root", "root"); File f = new File(saveFile); psmnt = connection.prepareStatement("insert into file(file_data) values(?)"); fis = new FileInputStream(f); psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length())); int s = psmnt.executeUpdate(); if(s>0) { System.out.println("Uploaded successfully !"); } else{ System.out.println("unsucessfull to upload file."); } } catch(Exception e){e.printStackTrace();} } %>
Thanks
Upload.html
Upload.jsp
String content=request.getContentType(); if(content!=null&&content.indexOf(multipart/formdata)); {
DataInputStream dt=new DataInputStream(request.getInputStream());
byte b[]=new byte[request.getContentLenght()];
}
the image is not inserting into database
Hi all, this is King, the 1st answer posted on November 12, 2010 at 3:02 PM is the most perfect answer i have ever seen. I would like to thank u a lot, altough i dont know who posted it. The 2 files page.jsp and upload.jsp works fine for me. What I did on my own is just altered the connectionURL and Class.forName since i am using Oracle 10g database and also created the folder UploadedFiles in C: drive, and had to create the database table file1 (since oracle dont allow you to name a table called file) with a colomn file_data (type BLOB). And dont forget to add commons-fileupload.jar and commons-io.jar to your lib. And then something happned which made me smile. I searched lots of sites for this file upload thing, bur never got the perfect answer. Thanks a ton ....
Iam using ms access and database extension is *.mdb.There when I save the attachment it is showing some other alphanumeric characters. I changed my databse to 2007 to include attachment. But the above code is not working for me. Kindly help