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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Ramsai Palchuri
Jsp Upload
2 Answer(s)      3 years and 9 months ago
Posted in : JSP-Servlet


<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>

Advertisement
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
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
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 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 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 - 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
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
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
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
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 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 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  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
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
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
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 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
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
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
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 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
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
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 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
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
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> <
 

 

 

DMCA.com