participate


JavaServer Pages (JSP) and JSTL - how to provide file browsing window at client side through jsp
This question is not answered.

<<   Back to Forum  |   Give us Feedback
This topic has 4 replies on 1 page.
jsweety
Posts:62
Registered: 3/27/08
how to provide file browsing window at client side through jsp   
Apr 7, 2008 7:02 PM
 
 
hi ,

I am using plain jsp and oracle 9i is my backend
i want to know how to provide a file browsing window to a client on click of a button
such that client should be able to upload a file in my database

thanx & regards

sweety
 
evnafets
Posts:10,798
Registered: 29/04/03
Re: how to provide file browsing window at client side through jsp   
Apr 7, 2008 7:28 PM (reply 1 of 4)  (In reply to original post )
 
 
<input type="file"> is the only html way to select a file from the client machine and upload it through a webpage.

That will provide the file browsing window, and do the upload.
For handling things at the server end, it is recommended to use a library such as the jakarta commons FileUpload component.
 
jsweety
Posts:62
Registered: 3/27/08
Re: how to provide file browsing window at client side through jsp   
Apr 7, 2008 7:43 PM (reply 2 of 4)  (In reply to #1 )
 
 
thanx evnafets
i am now trying that for uploading and inserting into database

cheers & regards

sweety
 
jsweety
Posts:62
Registered: 3/27/08
Re: how to provide file browsing window at client side through jsp   
Apr 7, 2008 10:07 PM (reply 3 of 4)  (In reply to #1 )
 
 
hi there
i have used following code for uploading file on my server
it is uploading (images,word files, txt files....) files properly
but it is very slow in updating
and i want to know how could then i insert it in my database table
please help me

<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>  
<%@ page import="java.io.FileWriter,java.io.IOException" %>  
<%@ page language="java" %> 
<%@ page import = "java.sql.*"%>
 
<%  
	String savePath = "", filepath = "", filename = "";  
	String contentType = "", fileData = "", strLocalFileName = "";  
	int startPos = 0, endPos = 0;  
	int BOF = 0, EOF = 0;  
%>  
<%!  
	//copy specified number of bytes from main data buffer to temp data buffer  
	void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)  
	{  
		for(int i=start;i<(start+len);i++)  
		{  
			toBytes[i - start] = fromBytes[i];  
		}  
	}  
%>  
<%	  
	contentType = request.getContentType();  
	out.println("<br>Content type is :: " +contentType);  
	if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))   
	{  
		DataInputStream in = new DataInputStream(request.getInputStream());  
		DataInputStream in1 = in;  
		int formDataLength = request.getContentLength();  
		byte dataBytes[] = new byte[formDataLength];  
		int byteRead = 0;  
		int totalBytesRead = 0;  
		while (totalBytesRead < formDataLength)  
		{	  
			byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);  
			totalBytesRead += byteRead;  
		}  
		out.println("<br>totalBytesRead : " + totalBytesRead + "    :   formDataLength = " + formDataLength);  
		  
		//String file = new String(dataBytes);   
		//out.println("<br>File Contents:<br>////////////////////////////////////<br>" + file + "<br>////////////////////////////////<br>");  
  
		byte[] line = new byte[128];  
		if (totalBytesRead < 3)	  
		{  
		  return;	//exit if file length is not sufficiently large  
		}  
  
		String boundary = "";  
		String s = "";  
		int count = 0;		  
		int pos = 0;  
		  
		//loop for extracting boundry of file  
		//could also be extracted from request.getContentType()  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf("Content-Disposition: form-data; name=\""); //set the file name  
			if(pos != -1)  
				endPos = pos;  
		}while(pos == -1);  
		boundary = fileData.substring(startPos, endPos);  
  
		//loop for extracting filename  
		startPos = endPos;  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf("filename=\"", startPos); //set the file name  
			if(pos != -1)  
				startPos = pos;  
		}while(pos == -1);					  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf("Content-Type: ", startPos);  
			if(pos != -1)  
				endPos = pos;						  
		}while(pos == -1);  
		filename = fileData.substring(startPos + 10, endPos - 3);	//to eliminate " from start & end  
		strLocalFileName = filename;  
		int index = filename.lastIndexOf("\\");  
		if(index != -1)  
			filename = filename.substring(index + 1);  
		else  
			filename = filename;  
		  
		//loop for extracting ContentType  
		boolean blnNewlnFlag = false;  
		startPos = endPos;	//added length of "Content-Type: "  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;			  
			pos = fileData.indexOf("\n", startPos);  
			if(pos != -1)  
			{  
				if(blnNewlnFlag == true)  
					endPos = pos;					  
				else  
				{  
					blnNewlnFlag = true;  
					pos = -1;  
				}  
			}  
		}while(pos == -1);  
		contentType = fileData.substring(startPos + 14, endPos);  
		  
		//loop for extracting actual file data (any type of file)  
		BOF = count + 1;  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf(boundary, startPos);	//check for end of file data i.e boundry value			  
		}while(pos == -1);  
		EOF = count - boundary.length();  
		//file data extracted  
  
		out.println("<br><br>0. Local File Name = " + strLocalFileName);  
		out.println("<br><br>1. filename = " + filename);  
		out.println("<br>2. contentType = " + contentType);  
		out.println("<br>3. startPos = " + BOF);  
		out.println("<br>4. endPos = " + EOF);  
		out.println("<br>5. boundary = " + boundary);  
  
		//create destination path & save file there  
		String appPath = application.getRealPath("/");  
		out.println("<br>appPath : " + appPath);  
		String destFolder = appPath + "images/";	//change this as required  
		filename= destFolder + filename;  
		FileOutputStream fileOut = new FileOutputStream(filename);  
		fileOut.write(dataBytes, BOF, (EOF - BOF));  
		fileOut.flush();  
		fileOut.close();  
		out.println("<br>File saved as >> " + filename);		  
		//file saved at destination  
		//out.println("<br>File data : <br><br>**************************<br>" + (new String(dataBytes,startPos, (endPos - startPos))) + "<br><br>**************************");  
 
	}  
	else  
	{  
		out.println("Error in uploading ");  
	}  
	  
%>



thanks & regards

sweety
 
evnafets
Posts:10,798
Registered: 29/04/03
Re: how to provide file browsing window at client side through jsp   
Apr 7, 2008 10:17 PM (reply 4 of 4)  (In reply to #3 )
 
 
Congratulations.
You've copied/pasted some code from somewhere on the internet that you probably don't understand.
Rather than using a library to do it for you :-)
At least with a library it makes your code understandable, even if you don't understand so much what its doing under the hood

Commons FileUpload

Using such a library would enable you to replace all of that code with something like:
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
 
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
 
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
 
 
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
 
    if (item.isFormField()) {
       // processFormField(item);
    } else {
       // processUploadedFile(item);
    }
}


With regards to inserting it into your database, it normally depends on your database.
You've shown you can google the answer for uploading files. Why stop there?

You should be able to write a method
public void saveBlobToDatabase(InputStream in)

That will work with Files, bytearrays... anything you can read with an InputStream.

Hope this helps,
evnafets
 
This topic has 4 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 25
  • Guests : 132

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums