CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Java

 file uploading using JSP

Print topic Send  topic

Author Message
Sheetal
Posted: 10/28/2005, 12:01 AM

I have tried using the code as below for file uploading.When run,it gives me an error as java.lang.IndexOutOfBoundsException,PLease help


The code is:

<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\ROOT\\Extrusions\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " +saveFile);
}
%>
eserver221
Posted: 10/30/2005, 11:54 PM

I recommend your one Book to read, you will find solution in Chapter 13.

Java for the Web with Servlets, JSP, and EJB: A Developer's Guide to J2EE Solutions
By Budi Kurniawan

Publisher : New Riders Publishing
Pub Date : April 12, 2002
ISBN : 0-7357-1195-X
Pages : 976


fun2oos

Posts: 4
Posted: 11/03/2005, 2:08 AM

jsp file upload code (any type of file): (fileUpload.jsp)



  
<%@ page  
import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.Servle  
tInputStream" %>  
<%@ page import="java.io.FileWriter,java.io.IOException" %>  
<%  
 String savePath = "", filepath = "", filename = "";  
 String contentType = "", fileData = "", strLocalFileName = "";  
 int startPos = 0;  
 int endPos = 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;  
  }  
 }  
%>  
<%  
 int BOF = 0, EOF = 0;  
 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)  
  startPos = 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);  
  endPos = 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 = " + startPos);  
  out.println("<br>4. endPos = " + endPos);  
  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/banner/";  
  filename= destFolder + filename;  
  FileOutputStream fileOut = new FileOutputStream(filename);  
  fileOut.write(dataBytes, startPos, (endPos - startPos));  
  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 ");  
 }  
  
%>  


supporting html file:

  
<form method="post" action="fileUpload.jsp" name="upform"  
enctype="multipart/form-data">  
  <table width="60%" border="0" cellspacing="1" cellpadding="1"  
align="center" class="style1">  
    <tr>  
      <td align="left"><b>Select a file to upload :</b></td>  
    </tr>  
    <tr>  
      <td align="left">  
        <input type="file" name="uploadfile" size="50">  
        </td>  
    </tr>  
    <tr>  
      <td align="left">  
  <input type="hidden" name="todo" value="upload">  
        <input type="submit" name="Submit" value="Upload">  
        <input type="reset" name="Reset" value="Cancel">  
        </td>  
    </tr>  
  </table>  
</form>  
</body>  
</html>  

_________________
fun2oos
View profile  Send private message
Jagadeesha M.V
Posted: 11/06/2005, 11:36 PM

how to store the uploading file in data base (MS-Access), i tried this code but this is storing data in specified path only so please help me

this is code
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());

//out.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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:\\temp\\jaga\\"+ saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

out.println("File saved as " +saveFile);

}
%>
Venkat
Posted: 11/07/2005, 3:19 PM

// Hi Fnds,
//This is the Simple Code that works well to upload any File Checkout


//Html Code for Browsing Purpose
<html>
<head>
<form action="NewUpload.jsp" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="filename" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>

//**************************************************************************//
// this is the JSP Code//


<%@ page import="java.util.*,java.io.*"%>

<%
String path=request.getParameter("filename");
String newPath="";
int count=0;

if(path!=null)
{
ArrayList arr=new ArrayList();
StringTokenizer st=new StringTokenizer(path,"\\");
while(st.hasMoreTokens())
{
arr.add(count,st.nextToken());
count++;
}
// create ur own path

newPath="c:/Ravii/"+arr.get(count-1);
int c;
FileInputStream fis=new FileInputStream(path);
FileOutputStream fos=new FileOutputStream(newPath);
while((c=fis.read())!=-1)
{
fos.write((char)c);
}
}

out.println("Thanks for using");
out.println("<br>");
out.println("<br>");
out.println("1.File1 Uploaded from :: "+path);
out.println("<br>");
out.println("<br>");
out.println("2.Uploaded File1 is Saved in :: "+newPath);
%>

//Contact:Vellabh@yahoo.com or raviii_4u@yahoo.com for queries

vidyasagar

Posts: 7
Posted: 11/29/2005, 12:55 AM

Hi

i think you will find best File Upload Source code in below link


http://www.javazoom.net/jzservlets/servlets.html

all the best
_________________
Thank you.
vidya sagar
View profile  Send private message
atul chavan
Posted: 12/22/2005, 6:15 AM

hi friends;
plz tell me how to save image in MS-Access using jsp
Javed Attar
Posted: 12/28/2005, 1:42 AM

I have tried using the code as below for file uploading.When run,it gives me an error as java.lang.IndexOutOfBoundsException,

one thing is when i run this jsp on server machine it works fine
but for any client sometimes it is giving result sometimes it is throwing above Exception

PLease help
please send me reason of this.
if possible then send me soln.
thanks in advance:-{}

Javed Attar
Posted: 12/28/2005, 1:43 AM

I have tried using the code as below for file uploading.When run,it gives me an error as java.lang.IndexOutOfBoundsException,

one thing is when i run this jsp on server machine it works fine
but for any client sometimes it is giving result sometimes it is throwing above Exception

PLease help
please send me reason of this.
if possible then send me soln.



<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
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:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\ROOT\\Extrusions\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " +saveFile);
}
%>

thanks in advance:-{}

krishna
Posted: 12/30/2005, 12:02 AM

hai
please tell me how to send,delete,compose emails using jsp& what are the specifications.
chetan
Posted: 01/13/2006, 2:30 AM

HI there

can anybody tell me the code to store the images in the MS access database....
if yes please send me the code regarding this ASAP, waiting for the reply.


regards
Chetan
ganesanvadivel
Posted: 02/07/2006, 11:33 AM

how to store a file in database
ganesanvadivel
Posted: 02/07/2006, 11:36 AM

send me how to store a file in database
shravasti
Posted: 03/02/2006, 10:30 PM

hi
please tell me, how to store a file or image in MS-Access using jsp and servlet and also tell me how to paste the contents of the file into textarea
using jsp, waiting for the reply
venkat
Posted: 03/15/2006, 9:18 PM

i want explanation of every line of the below code

because as i am new to jsp

i want proper explanation as soon as u can

because i want to do one module for uploading and downloading files

very urgent

thanking u

yours sincerely
venkat

Quote fun2oos:
jsp file upload code (any type of file): (fileUpload.jsp)



  
<%@ page  
import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.Servle  
tInputStream" %>  
<%@ page import="java.io.FileWriter,java.io.IOException" %>  
<%  
 String savePath = "", filepath = "", filename = "";  
 String contentType = "", fileData = "", strLocalFileName = "";  
 int startPos = 0;  
 int endPos = 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;  
  }  
 }  
%>  
<%  
 int BOF = 0, EOF = 0;  
 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)  
  startPos = 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);  
  endPos = 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 = " + startPos);  
  out.println("<br>4. endPos = " + endPos);  
  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/banner/";  
  filename= destFolder + filename;  
  FileOutputStream fileOut = new FileOutputStream(filename);  
  fileOut.write(dataBytes, startPos, (endPos - startPos));  
  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 ");  
 }  
  
%>  


supporting html file:

  
<form method="post" action="fileUpload.jsp" name="upform"  
enctype="multipart/form-data">  
  <table width="60%" border="0" cellspacing="1" cellpadding="1"  
align="center" class="style1">  
    <tr>  
      <td align="left"><b>Select a file to upload :</b></td>  
    </tr>  
    <tr>  
      <td align="left">  
        <input type="file" name="uploadfile" size="50">  
        </td>  
    </tr>  
    <tr>  
      <td align="left">  
  <input type="hidden" name="todo" value="upload">  
        <input type="submit" name="Submit" value="Upload">  
        <input type="reset" name="Reset" value="Cancel">  
        </td>  
    </tr>  
  </table>  
</form>  
</body>  
</html>  
anonymous
Posted: 03/19/2006, 8:56 PM

please show me complete code on how to store images into oracle using JSP(java server pages). we're developing executive dashboard which needs to store lots and displaying lots of images
madhu
Posted: 03/27/2006, 4:37 AM

hi
i tryed the fileUpload.jsp.but iam getting one error at toBytes[i - start] = fromBytes; i.e,ERROR: fileUpload.jsp: The type of the operands of this assignment are not assignment-convertible.
please help me.its urgent
madhumati
Posted: 03/27/2006, 4:39 AM

hi
i tryed the fileUpload.jsp.but iam getting one error at toBytes[i - start] = fromBytes; i.e,ERROR: fileUpload.jsp: The type of the operands of this assignment are not assignment-convertible.
please help me.its urgent
<%@ page
import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.Servle
tInputStream" %>
<%@ page import="java.io.FileWriter,java.io.IOException" %>
<%
String savePath = "", filepath = "", filename = "";
String contentType = "", fileData = "", strLocalFileName = "";
int startPos = 0;
int endPos = 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;
}
}
%>
<%
int BOF = 0, EOF = 0;
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)
startPos = 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);
endPos = 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 = " + startPos);
out.println("<br>4. endPos = " + endPos);
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/banner/";
filename= destFolder + filename;
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.write(dataBytes, startPos, (endPos - startPos));
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 ");
}

%>
Rakesh Vashisht
Posted: 05/03/2006, 4:09 AM

Great!!!!!!!!!!! stuff,

i also have a new way for uplaoding , i wil post that code soon... so that guys can have a new solution for uplaoding files.

saviola
Posted: 05/19/2006, 5:25 AM

hello
how are you ???
tell me i'm a developper
i just the first time use this software(CodeCharge Studio), i want creat application with jsp and servlet but i don't how can i do it please if u can send me some lessons or how to resolve my problem, thnxs
bye
shekar
Posted: 05/24/2006, 8:32 AM

Hi Frnds,
I am really thankful for giveing me a simple prog to upload file from client side.
once again very thankful
bye
bicer

Posts: 6
Posted: 07/06/2006, 12:37 AM

i use fileUpload.jsp but much slow that transferring a image when i was pressed upload button
pls help me :-(
_________________
BBBBBBBBBBB
View profile  Send private message
senthil
Posted: 08/09/2006, 9:32 PM

Quote madhu:
hi
i tryed the fileUpload.jsp.but iam getting one error at toBytes[i - start] = fromBytes; i.e,ERROR: fileUpload.jsp: The type of the operands of this assignment are not assignment-convertible.
please help me.its urgent


hi,

toBytes[i - start] = fromBytes;
append this into ur code ,it will work
Rocio
Posted: 08/24/2006, 11:55 PM

Quote Rakesh Vashisht:
Great!!!!!!!!!!! stuff,

i also have a new way for uplaoding , i wil post that code soon... so that guys can have a new solution for uplaoding files.



Hello Rakesh,
Can you please post your solution. Would really appreciate it.
Thanks
Sarah
Posted: 08/25/2006, 12:24 AM

fun2oos
Your code works well :-), except that it adds two extra bytes to the new file. Does anyone know why? If possible, can you add more comments to code. I really want to understand it.

Thanks a bunch.

Quote fun2oos:
jsp file upload code (any type of file): (fileUpload.jsp)



  
<%@ page  
import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.Servle  
tInputStream" %>  
<%@ page import="java.io.FileWriter,java.io.IOException" %>  
<%  
 String savePath = "", filepath = "", filename = "";  
 String contentType = "", fileData = "", strLocalFileName = "";  
 int startPos = 0;  
 int endPos = 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;  
  }  
 }  
%>  
<%  
 int BOF = 0, EOF = 0;  
 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)  
  startPos = 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);  
  endPos = 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 = " + startPos);  
  out.println("<br>4. endPos = " + endPos);  
  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/banner/";  
  filename= destFolder + filename;  
  FileOutputStream fileOut = new FileOutputStream(filename);  
  fileOut.write(dataBytes, startPos, (endPos - startPos));  
  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 ");  
 }  
  
%>  


supporting html file:

  
<form method="post" action="fileUpload.jsp" name="upform"  
enctype="multipart/form-data">  
  <table width="60%" border="0" cellspacing="1" cellpadding="1"  
align="center" class="style1">  
    <tr>  
      <td align="left"><b>Select a file to upload :</b></td>  
    </tr>  
    <tr>  
      <td align="left">  
        <input type="file" name="uploadfile" size="50">  
        </td>  
    </tr>  
    <tr>  
      <td align="left">  
  <input type="hidden" name="todo" value="upload">  
        <input type="submit" name="Submit" value="Upload">  
        <input type="reset" name="Reset" value="Cancel">  
        </td>  
    </tr>  
  </table>  
</form>  
</body>  
</html>  
:-)
Janice

Posts: 2
Posted: 09/14/2006, 8:20 PM

Hi! I am doing on a function whereby you need to
1) do a UI
2) check if the file exists in the client
3) Upload the file to the server for example : c:/temp
5) read each row
6) for each row, get the data
7) update the database accordingly
View profile  Send private message
Janice

Posts: 2
Posted: 09/14/2006, 8:22 PM

can anyone tell how to code it?
the file i have is jsp, action, form, bean, event, eventhandler, DAO, module, and those struts
View profile  Send private message
bparthi

Posts: 1
Posted: 09/15/2006, 3:10 AM

Quote Sarah:
fun2oos
Your code works well :-), except that it adds two extra bytes to the new file. Does anyone know why? If possible, can you add more comments to code. I really want to understand it.

Thanks a bunch.

Quote fun2oos:
jsp file upload code (any type of file): (fileUpload.jsp)



  
<%@ page  
import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.Servle  
tInputStream" %>  
<%@ page import="java.io.FileWriter,java.io.IOException" %>  
<%  
 String savePath = "", filepath = "", filename = "";  
 String contentType = "", fileData = "", strLocalFileName = "";  
 int startPos = 0;  
 int endPos = 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;  
  }  
 }  
%>  
<%  
 int BOF = 0, EOF = 0;  
 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)  
  startPos = 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);  
  endPos = 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 = " + startPos);  
  out.println("<br>4. endPos = " + endPos);  
  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/banner/";  
  filename= destFolder + filename;  
  FileOutputStream fileOut = new FileOutputStream(filename);  
  fileOut.write(dataBytes, startPos, (endPos - startPos));  
  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 ");  
 }  
  
%>  


supporting html file:

  
<form method="post" action="fileUpload.jsp" name="upform"  
enctype="multipart/form-data">  
  <table width="60%" border="0" cellspacing="1" cellpadding="1"  
align="center" class="style1">  
    <tr>  
      <td align="left"><b>Select a file to upload :</b></td>  
    </tr>  
    <tr>  
      <td align="left">  
        <input type="file" name="uploadfile" size="50">  
        </td>  
    </tr>  
    <tr>  
      <td align="left">  
  <input type="hidden" name="todo" value="upload">  
        <input type="submit" name="Submit" value="Upload">  
        <input type="reset" name="Reset" value="Cancel">  
        </td>  
    </tr>  
  </table>  
</form>  
</body>  
</html>  
:-)
Hi,

I am get error as :
incompatible types
found : byte[]
required: byte
toBytes[i - start] = fromBytes;
What shall i do to slow this problem?? Please very urgent. Reply...
View profile  Send private message
moh123

Posts: 2
Posted: 11/23/2006, 5:06 AM

hello everyone,

just wanted to ask when u upload a file to the server side,
is it necessary to read the contents of the file or you could just save the file onto the
server side...
if it is possible to just save the file on the server without reading the contents
then plz could someone tell me how to do this,

thanks in advance
moh
View profile  Send private message
srikanth

Posts: 2
Posted: 11/26/2006, 5:37 AM

hello everyone

Can any one give me a code for uploading an image with jsp.
please..
thanks in advance
_________________
M.Srikanth
View profile  Send private message
 Page 1 of 2  Next Last


Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.