Jsp Upload

Jsp Upload

<p>multipart\form-data; boundary=----WebKitFormBoundaryq38tlHi55oATzZ7S (The system cannot find the path specified)
I had got an error when  Uploading to an database Mysql
the code is given below</p>

<p><html><body>
&lt;%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
&lt;%@ page import="java.io.*" %>
&lt;%</p>

<pre class="prettyprint">String contentType = request.getContentType();
FileInputStream fis;

if ((contentType != null) &amp;&amp; (contentType.indexOf("multipart/form-data") &gt;= 0)) {
    File image=new File(contentType);


    PreparedStatement psmt=null; 
    Class.forName("org.gjt.mm.mysql.Driver");

    Connection con = 
      DriverManager.getConnection(
        "jdbc:mysql://localhost:4040/", "root", "hp");
        psmt=con.prepareStatement("insert into gallery values(?)"); 
    fis=new FileInputStream(image); 


        psmt.setBinaryStream(3, (InputStream)fis, (int)(image.length())); 

        int s = psmt.executeUpdate(); 
        if(s&gt;0) {
</code></pre>

<p>%> 
<b><font color="Blue"> 
&lt;% out.println("Image Uploaded successfully !"); %> 
</font></b> 
&lt;% 
}</p>

<p>else { </p>

<p>out.println("unsucessfull to upload image."); </p>

<p>}</p>

<p>con.close();</p>

<p>psmt.close();
    }
%>
</html></body></p>
View Answers

April 6, 2011 at 10:29 AM

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.*" %>
<%
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();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</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("Error!");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

For the above code, we have created the following table:

CREATE TABLE `file` (                                    
          `id` bigint(50) NOT NULL auto_increment,               
          `file_data` longblob,                                  
          PRIMARY KEY  (`id`)                                    
        );

May 17, 2013 at 7:22 PM

Hello, thnks you ! your exemple was very helpful, but how we can upload the files just uploaded ??? and thnks you very much have a good day :)









Related Tutorials/Questions & Answers:
Jsp Upload
Jsp Upload  <p>multipart\form-data; boundary...; <p>else { </p> <p>out.println("unsucessfull to upload...;<TITLE>Display file upload form to the user</TITLE></HEAD>
jsp upload file to server
jsp upload file to server  How to create and upload file to server in JSP?   Find the given example that explains how to upload single and multiple file on server using JSP
Advertisements
upload images - JSP-Servlet
upload images  How to write the jsp code to upload images...://www.roseindia.net/jsp/upload-insert-csv.shtml Thanks... to upload any file therefor it can also upload an image and can insert
Image upload file - JSP-Servlet
Image upload file  I want a code for image upload jsp or servlet.  Hi friend, For image upload jsp or servlet visit to : http://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml http
Upload the picture - JSP-Servlet
://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml Thanks RoseIndia...Upload the picture  Dear Sir, How to upload the picture in the database a webpage and i want to show that picture another page. what i
Upload and Download in JSP - JSP-Servlet
and downloading a file in JSP.. When the admin clicks upload, he must be able to upload.... Please visit for more information. http://www.roseindia.net/jsp/file_upload...Upload and Download in JSP  Respected Sir/Madam, I am
upload to database - JSP-Servlet
upload to database  hai friends i have a query that is i have to upload a pdf file into database(sqlserver2000) using jsp. In roseindia some examples i seen that is only for uploading into the server but i need the uploaded file
upload - JSP-Servlet
Upload jar file  What is the steps to upload Jar file in Java
Multiple upload - JSP-Servlet
Multiple upload  Hello everyone and Deepak i am using jsp and mysql I am using the program published on roseindia.net of Multiple upload and i am facing an error as given below please help and reply soon this is my 8th
upload and download files - JSP-Servlet
and download files in JSP visit to : http://www.roseindia.net/jsp/file_upload...upload and download files  HI!! how can I upload (more than 1 file) and download the files using jsp. Is any lib folders to be pasted? kindly
File upload - JSP-Servlet
File upload  I am trying to do a file upload program. But, it shows... ----------------------------------------------------------------------------- Display file upload form to the user <... the file To Upload
File upload - JSP-Servlet
File Uload Using JSP   Choose the file To Upload...File upload  Hello Friends, In my JSP project i want to do a file upload part. For this, i designed two files one for input and other
Photo Upload - JSP-Servlet
for image upload and download using Servle/jsp. Thanks&Regards, VijayaBabu.M  Hi <% //to get the content type information from JSP Request...(); %> You have successfully upload the file by the name
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
upload csv to mysql db using jsp upload
upload csv to mysql db using jsp upload  Hello all, Please give me the code to uplad .csv from jsp page and insert values into MySQl db. I have a table with 8 cloumns.(MDN--varchar(30),Otafdate date,crt varchar(30),dmdn
File upload in JSP
File upload in JSP  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
upload image using JSP Hibernate
upload image using JSP Hibernate  sir, I want to take image from user and save to database(MYSQL) using Hibernate and JSP Thanks in advance
how to upload and download images using buttons in jsp?
how to upload and download images using buttons in jsp?  how to upload and download images using buttons in jsp
How to upload files to server using JSP/Servlet?
How to upload files to server using JSP/Servlet?  How to upload files to server using JSP/Servlet
load and upload. - JSP-Servlet
load and upload.  dear sir, plz give me the sol for my problem.i m waiting for u r reply. why u not replying ..   Hi Friend, We haven't remember your problem. So please send it again. Thanks
File Upload - JSP-Servlet
File Upload  Hi everyone, I am facing file uploading problem.the multiple file upload code of roseindia is working on localhost but the same code is not working on server.i think the package commons-fileupload-1.2.jar
Upload and Download Large files in jsp
Upload and Download Large files in jsp  I am not able to download large files (>200mb) from any server. I need a code to download and upload large files (atleast 4 gb) to a server using jsp page
File Upload And download JSP Urgent - JSP-Servlet
File Upload And download JSP Urgent  Respected Sir/Madam, I... for it.. In the same application, I need the coding for File Upload and File Download in JSP.. In the Admin window, There must be "Upload" provision where admin
file upload error - JSP-Servlet
file upload error  Hello friends, In my project when i am trying to upload a file i used the roseindia.net coding from URL: http://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml
image upload with jsp from form
image upload with jsp from form  hi i used the code specified in your following post http://www.roseindia.net/answers/viewqa/JSP-Servlet/9749-image...) org.apache.jsp.upload_jsp._jspService(upload_jsp.java:85
File Upload Tutorial With Examples In JSP
File Upload Tutorial With Examples In JSP  ... by Using JSP This tutorial will help you to understand how you can upload a file... in the developing the project in which you have to upload any type of files
file upload error - JSP-Servlet
file upload error  Iam doing jsp project. File uploading is one part of the project. In this particular part i got an error... ----------- file upload example
file upload in jsp - Java Beginners
file upload in jsp  how to upload a file using jsp. my operating...: Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload:   2
Multiple image upload using JSP
Multiple image upload using JSP  I have written a code to upload the multiple images using jsp, but when I execute this code, it throws Corrupt form...="text/html; charset=utf-8" /> <FORM action="upload_jsp.jsp" enctype
file upload in jsp - Java Beginners
file upload in jsp  how to upload a file in jsp and store it in my project directory  Hi Friend, We have modified 'upload.jsp...)); fileOut.flush(); fileOut.close(); %>You have successfully upload
Upload and download file - JSP-Servlet
Upload and download file  What is JSP code to upload and download a document in a web page?  Hi Friend, Try the following code to upload the word document file: 1)page.jsp Display file upload form
file upload error - JSP-Servlet
file upload error  Iam doing jsp project. File uploading is one part of the project. In this particular part i got an error as ----------------------------------------------------------------------------- org.apache.jasper.JasperException
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... file upload form to the user</TITLE></HEAD> <
File Upload in FTP - JSP-Servlet
File Upload in FTP  hi sir, i am doing upload files in FTP using java application. is there any jar file to use the privileges of sun.net.ftp. i am writing my program in java servlet. i am getting errors like 1.cannot Find
JSP Upload and Downloading files - JSP-Servlet
JSP Upload and Downloading files  Respected Sir/Madam, Very... and downloading files in JSP, I am facing a problem.. 1) The file name is getting inserted... reference: For Upload: <% int val =0; String contentType
file upload and insert name into database - JSP-Servlet
  HIread for more information,http://www.roseindia.net/jsp/file_upload...file upload and insert name into database  Hi, I just joined as a fresher and was given task to upload a file and insert its name into database
how to upload a file - JSP-Servlet
how to upload a file  Dear sir, Give me some code...: Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload:   2
Image upload in mysql database using jsp servlet
Image upload in mysql database using jsp servlet  Hello, I need code... located inside the application, but i need to upload in mysql and also for inserting different format of file to upload into mysql db like pdf and doc file
FTP FILE UPload - JSP-Servlet
FTP FILE UPload  Hi sir, i am doing the file upload via ftp. the program compiled well . but while executing i am getting the below errors. exception javax.servlet.ServletException: Servlet execution threw an exception
upload a file and write it in JSP using servlet
upload a file and write it in JSP using servlet  Hello, I'm facing a problem here. I want to upload a file through abc.jsp and write the contents of file using a servlet in xyz.jsp. It is supposed to be a excel file which
Upload Exce Data into MySql Using Jsp and Servlet - JSP-Servlet
Upload Exce Data into MySql Using Jsp and Servlet  now i am doing a project my concept is to insert the Excel File Data is uploaded and inserted... the following link: http://www.roseindia.net/jsp/upload-insert-csv.shtml Hope
insert name city and upload image in database using mysql and jsp
insert name city and upload image in database using mysql and jsp   insert name city and upload image in database using mysql and jsp
upload and retrieve a word document - JSP-Servlet
; 2.Create a form in jsp page "page.jsp" : Display file upload form to the user...:     3.Create a jsp page "upload_page.jsp...upload and retrieve a word document  Hi, I have a problem
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image and store that image in a database
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image and store that image in a database
how to upload image from jsp to mssql
how to upload image from jsp to mssql  hi there!!, i'm using jsp and servlet to upload images to the database. however i have difficulty in uploading...;tr> <center><td colspan="2"><B>UPLOAD THE FILE</B><
how to upload image from jsp to mssql
how to upload image from jsp to mssql  hi there!!, i'm using jsp and servlet to upload images to the database. however i have difficulty in uploading...;tr> <center><td colspan="2"><B>UPLOAD THE FILE</B><
Read code from excel sheet and upload into database using JSP
Read code from excel sheet and upload into database using JSP  I want to upload data to database from an excel worksheet using jsp ...Please help
File Upload and Down load - JSP-Servlet
File Upload and Down load  Gud afternoon deepak, In my project i need to upload a file and have to save the file path in the database. again i need to receive by giving the path. how could i save and receive the path
how do i upload a file by using servlet or jsp?
how do i upload a file by using servlet or jsp?  hi plz tell me the write java code

Ads