Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet file uploading using jsp

Have Programming Question? Ask it here!
 
 
 


yeswanth kumar
file uploading using jsp
1 Answer(s)      3 years and 7 months ago
Posted in : JSP-Servlet


below file uploading code has one error "toBytes[i-start]=fromBytes; " incompatable type error please help me how to solve this error

<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>
<%@ page import="java.io.FileWriter,java.io.IOException" %> 
<% 
 String savePath = "",filepath="",filename="";
 String contentType ="", fileData = "", strLocalFileName = ""; 
 int startPos = 0; 
 int endPos = 0; 
%> 
<%!
 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("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("totalBytesRead:"+totalBytesRead +":formDataLength = " + formDataLength); 

 //String file = new String(dataBytes); 
  //out.println("FileContents:" + file +"");
  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 datai.e boundry value 
  }while(pos == -1);
  endPos = count - boundary.length(); 
  //file data extracted 

  out.println("0. Local File Name = " + strLocalFileName);
  out.println("1. filename = " + filename);
  out.println("2. contentType = " + contentType);
  out.println("3. startPos = " + startPos);
  out.println("4. endPos = " + endPos);
  out.println("5. boundary = " + boundary);

 //create destination path & save file there
  String appPath = application.getRealPath("/"); 
  out.println("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("File saved as >> " + filename);
  //file saved at destination 
  //out.println("File data : ******"+(new String(dataBytes,startPos, (endPos - startPos))) +"");
 } 
 else 
 {
  out.println("Error in uploading "); 
 }  

%>

Advertisement
View Answers

January 10, 2011 at 10:51 AM


Hi Friend,

Try the following code:

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=
"uploadandstore.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">&nbsp;</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">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)uploadandstore.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
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:/"+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:</b>
<% out.println(saveFile);%></td></tr></table>
<%
}
    %>

Thanks



Related Tutorials/Questions & Answers:
file uploading using jsp
file uploading using jsp  below file uploading code has one error... = " + formDataLength); //String file = new String(dataBytes); //out.println("FileContents:" + file +""); byte[] line = new byte[128]; if (totalBytesRead
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..   Visit Here
Advertisements
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored...;b>Choose the file To Upload:</b></td> <td><INPUT NAME
uploading a file at another system in lan using jsp
uploading a file at another system in lan using jsp  Thanks for the code at "http://www.roseindia.net/jsp/fileupload/Sinleupload.xhtml.shtml... to save or upload the file at another system in lan at location "http
Uploading a file using UploadBean
Uploading a file using UploadBean  Dear sir, In my project i have to upload the file and use the same file for getting a values from that uploaded .xls file.I used UploadBean for uploading .For the first time when i
File uploading - JSP-Servlet
File uploading  i am using file uploading code for multiple file and aslo for single file but i am getting problem that No such file found.... http://www.roseindia.net/jsp/file_upload/Sinle_upload.xhtml.shtml
video uploading using jsp
video uploading using jsp  how to upload a videos in web page using jsp   Hi, You can upload your video with the help of JSP file upload code. Once file is upload you can play using any video player. Get the code
Uploading tha file - JSP-Servlet
Uploading tha file  hi,Sir. this is siddaiah. please give me response for the follwing questions. 1.how to upload file into oracle database using JSP? 2.How to upload photo into oracle database using JSP? Regards
Uploading image using jsp
Uploading image using jsp  how to upload image using jsp. Already i tried, But that image file does not read. It returns only -1 without reading that image file ... I want know that solution using by u... Thanks, P.S.N.  
file uploading - JSP-Servlet
file uploading  Hi, thanks a lot for your kind answer. Now my program... problem. Im not geeting the full output for the program. Even, the file... to solve the problem as well. Thanks in advance. Input File
video uploading using jsp
video uploading using jsp  this is the code i hv written for uploading..but the value in dropdown list is not captured..can any 1 tell the reason html file <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN
FILE UPLOADING - JSP-Servlet
FILE UPLOADING  Hi , I want Simple program for file upload using html and servlet plese help me  hi friend pls try this code ********** try{ String type=""; String boundary=""; String sz
Multiple file Uploading - JSP-Servlet
Multiple file Uploading   Hello everyone I am using jsp and my IDE... class for JSP: An error occurred at line: 9 in the generated java file.... org.apache.commons.fileupload.disk.DiskFileItemFactory resolves to a package An error occurred at line: 19 in the jsp file
Uploading Single File by Using JSP
Uploading Single File by Using JSP  ... to understand how you can upload a file by using the Jsp. As Jsp is mainly used... by using Jsp and how it will get stored on a particular memory area. To get
Uploading Multiple Files Using Jsp
Uploading Multiple Files Using Jsp  ... to understand how you can upload multiple files by using the Jsp. We should avoid... by using Jsp and how they will get stored on the particular memory area. To get
uploading a file - JSP-Interview Questions
uploading a file   uploading a file and storing in database using...; public File currentDir; public DropTarget target; JButton addButton...,BorderLayout.SOUTH); currentDir = new File(System.getProperty("user.dir")); } public
Uploading Single File by Using JSP
Uploading Single File by Using JSP  ... to understand how you can upload a file by using the Jsp. As Jsp is mainly used... by using Jsp and how it will get stored on a particular memory area. To get
uploading a file
uploading a file  When I am trying to upload a file to another system in lan at a location "http://192.168.12.5:8080/tomcat-docs/myapps",then it is giving the following error message "http://192.168.12.5:8080/tomcat-docs/myapps
uploading audio file iphone
uploading audio file iphone  uploading audio file iphone
file uploading
file uploading  How to upload music files onto the server and save the file information to the mysql database fetch and play the music files on a display page without download thus streaming the files
Pass hidden veritable while uploading file - JSP-Servlet
are uploading file, all the parameter other then file upload are null. Is it write... fields while uploading file on server?" Thanks in advance...Pass hidden veritable while uploading file  Hi All, Is there any
File uploading - Ajax
File uploading  hi friends, how to uploading the file by using "AJAX".Please send the complete source code for this application   where u want to store the file Can u specify
urgent need for jsp code with mysql query for uploading and downloading file - JSP-Servlet
urgent need for jsp code with mysql query for uploading and downloading file  can anyone tell me how to upload and download a doc file using jsp with mysql as the back end... and kindly help me to create the table too
Uploading File on Server
Uploading File on Server  Hello, Can someone explain or suggest example. How do i uploading files on the FTP Server. Thanks
Struts file uploading - Struts
Struts file uploading   Hi all, My application I am uploading files using Struts FormFile. Below is the code. NewDocumentForm... it is breaking while uploading the large file. Please let me know if you know any API
uploading file to tomcat server
uploading file to tomcat server  please tell me how to upload a file to the URL "http://192.168.12.7:8000/tomcat-docs/" ? thanks
Uploading a .3gp file. - MobileApplications
Uploading a .3gp file.  sir, i m sending it again .i want code for java based mobile application, that can take .3GP video file and upload it to youtube using youtube's APIs. The video file would be located on a weblink
query related to uploading file
query related to uploading file  hello friends i am doing my project in servlet and i want to upload a file and store file in local hard drive and file name and path in oracle database.with name file name and path i also want
uploading a text file into a database
uploading a text file into a database  how to upload a text file into a database using jchooser   import java.io.*; import java.sql.... { static File file; public static void main(String[] args) throws Exception
Using a image for Browse button instead of normal html Browse button for Uploading files from a JSP - JSP-Servlet
Using a image for Browse button instead of normal html Browse button for Uploading files from a JSP  I am using the following code snippet : The above code snippet is able to open the File Chooser. There is also
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
Uploading an image - JSP-Servlet
. But, it will not check for file size. File path getting had done by a different jsp page and uploading is done by different jsp coding. I don't know how to code for file size...Uploading an image  I am doing a jsp project. In this uploading
Drag and drop file uploading - Ajax
Drag and drop file uploading  Hi all, This is NageswaraRao i want file uploading feature on my web development..using drag and drop mouse functionality. Problem:I have Created one Text area when i drop the file on text area
file upload using JSP
file upload using JSP  I have created a form to upload a file in a html page, now i want to get the path of the file in a jsp page so what code...="java" %> <HTML> <HEAD><TITLE>Display file upload form
file uploading - Java Beginners
file uploading   hi, i am developing one stand alone j2se programming in that i have to upload a file to another system in the LAN and have to download from the server .i am using java.io. and java.net and swing .the task
PHP error uploading file - PHP
PHP error uploading file  I am getting error while uploading a file in a folder in PHP ... Warning: Cannot modify header information - headers already send any idea
file uploading - JavaMail
file uploading  Hi thi is swathi.Thank s for giving the answers to previous questions.I am getting the problem with below code FileItemFactory... using this code i am getting the following exception,actually this code is working
file Uploading - Development process
file Uploading  Hi all, This is the problem i am facing please help me and solve the problem.i want upload file from my localpc(computer)to any textarea in webapplication.when i drop the mouse on text area the file content
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
How to upload file using JSP?
How to upload file using JSP?   Hi all, I m the beginner in JSP, I want to upload file on server in specific folder.   1)page.jsp...;% // for uploading the file we used Encrypt type of multipart/ form-data and input of file type
Uploading the file on Server.
Uploading the file on Server This example will explain you how to upload... on the server. For uploading a file onto the FTP server, first you have to login... the connection of FTP server for user name. upload the file using ftp_put() function
excel uploading in jsp
excel uploading in jsp  could you provide the source code for: 1)have to upload an empty excel sheet at client side i.e if client clicks an excel... given and printing them in a jsp page. could you please provide the code in spring
Uploading a Profile with image[file] into a sql database
Uploading a Profile with image[file] into a sql database  I need to upload a file along with the some text into a database ......[ Similar to http://www.roseindia.net/jsp/jspfileupload/employeeuploadprofile_image.shtml
Uploading Multiple Files Using Jsp
Uploading Multiple Files Using Jsp  ... to understand how you can upload multiple files by using the Jsp. We should avoid... by using Jsp and how they will get stored on the particular memory area. To get
FTP Server : Upload file
This tutorial contains description of file uploading to the FTP server using java
Uploading Excel sheet record in JSP to insert data in MySql
Uploading Excel sheet record in JSP to insert data in MySql  Need Help how to upload Excel (.xls) file and insert data in Mysql using JSP it wil be wonder for me if any help me
What changes I have to do in php.ini file for file uploading?
What changes I have to do in php.ini file for file uploading?  What changes I have to do in php.ini file for file uploading
Uploading a software and displaying it on the jsp page
Uploading a software and displaying it on the jsp page  I have a Downloads page in my website, I want it to display the name of softwares as soon as it is uploaded in the backend by the administrator. And the admin may be a non
Advertisements
 

 

 

DMCA.com