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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


surendra sharma
Hello
2 Answer(s)      4 years and a month ago
Posted in : JSP-Servlet


Hello sir i want to store upload doc file in ms access by using servlet. Can i store file in access.one another things access only text size is 255 character but my file is up to 2mb how i can store.


Advertisement
View Answers

November 25, 2010 at 12:37 PM


Hi Friend,

We are providing you the code, that will upload and insert any file to Mysql database. In Mysql, we take long blob as the datatype for document file.

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">&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)upload.jsp:

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

<% 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);
String 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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</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(?)");
//psmnt.setString(1,saveFile);
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("unsucessful to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

If you want to insert into msaccess database, then use OLE Object as the datatype for docuement file.

Thanks



November 25, 2010 at 12:37 PM


Hi Friend,

We are providing you the code, that will upload and insert any file to Mysql database. In Mysql, we take long blob as the datatype for document file.

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">&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)upload.jsp:

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

<% 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);
String 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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</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(?)");
//psmnt.setString(1,saveFile);
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("unsucessful to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

If you want to insert into msaccess database, then use OLE Object as the datatype for docuement file.

Thanks



Related Tutorials/Questions & Answers:
hello
Java Error in Hello World  Java Error in Hello World
hello
Java Hello World  HELLO World program in Java
Advertisements
Hello
Hello  Hello sir i want to store upload doc file in ms access by using servlet. Can i store file in access.one another things access only text size is 255 character but my file is up to 2mb how i can store
Hello ..
Hello ..  Hello, I need a code for .. Want to read csv file (which is having name nd mobile number) from jsp and if i give search by name mobile number should get displayed and if i give mobile number name should get
hello
there is so many error...please try check it out..   Hello Friend, We have
hello
character is vowel or not.   Hello Friend, Try the following code
Hello Eyeryone...
Hello Eyeryone...  how to download java material in roseindia.net website material please kindly help me... by visu
Hello world
Hello world (First java program)   ... and can be run on any operating System. Writing Hello World program is very simple. To write the Hello world program you need  simple text editor like note
print hello n hi
print hello n hi  how to write a java program that prints "hello" 5 times, "hi" 1 time n again "hello" 4 times..?? do reply
Hello world program
Hello world program  hello world program   class Hello{ public static void main(String[] args) { System.out.println("Hello World"); } } For more information, visit the following link: http
hello in vertical manner
hello in vertical manner  How to print HELLO as in vertical manner   Hi, Try this: class HelloExample { public static void main(String[] args) { String str="HELLO"; char ch[]=str.toCharArray
Hello World in servlet
Hello World in servlet  Hello World example in servlet ?   import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public..., IOException { PrintWriter out = res.getWriter(); out.println("Hello
Hello World - Java Beginners
Hello World  Java Beginner - 1st day. Looked at the Hello World script and thought I would give it a try....I created the script in Notepad. Saved...){ System.out.println("Hello World!"); } } what am I doing wrong. Am I expecting
J2ME Hello World Example
J2ME Hello World Example       This is the simple hello world application. In this example we are creating a form name "Hello World" and creating a string message "
Hello - Java Beginners
Hello  Hello I have a two button update and delete I want to user click on update button then msg will be displayed in msg box Are u sure want to update If user click yes then all record will be updated
Hello World Program
Hello World Program  write a java program that continuously prints HelloWorld! to the screen(once every second ) and exists when press the enter key... = new DisplayMessageContinuously("Hello World ", 1000); th.start
EJB Hello world example
EJB Hello world example       Creating and testing the "Hello World" Example is the very first... example we are going to show you, how to create your first hello world example
hello there i need help
hello there i need help  : i need to do a program like this: Automatic Teller Machine [B] Balance [D] Deposit [W] Withdrawal [Q] Quit select you OPtions: once i have chosen an option then i should proceed here if i choose b
pring Hello World Application
Spring Hello World Application       Hello World Example using Spring, The tutorial given below describes you the way to make a spring web application that displays Hello World
Java Hello World code example
Java Hello World code example  Hi, Here is my code of Hello World program in Java: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World
java hello and timer alert application
java hello and timer alert application  Hi, Please help me create a simple Java application that can display "Hello world" and current Time alert on mobile device screen using mobile screen emulator. Regards
TIBCO Designer Tutorial - Hello World
TIBCO Designer Tutorial - Hello World  This TIBCO tutorial teaches you how to create a process definition that writes the words "Hello, World... output "Hello, World!" into the file. This task requires knowledge of the file
jQuery Hello World
jQuery Hello World example     ... application called "Hello World jQuery". This application will simply display...'s start developing the Hello World application in jQuery. Video Tutorial
Smarty Hello World Program
How to write "Hello World" program? In any smarty program we need two files: a) .php file and b).tpl file.     i) .php file: Which...;//enable the caching   $smarty->assign ('name', 'hello world
Spring Hello World prog - Spring
Spring Hello World prog  I used running the helloworld prog code mentioned in http://www.vaannila.com/spring/spring-mvc-tutorial-1.html I'm getting null pointer exception. as shown below. I added all the jars and my
Simple Procedure to display Hello World
Simple Procedure to display Hello World   ... that helps you to display 'Hello World'. In this Example, we create a procedure... you the 'Hello,World' ,whenever a procedure 'abc' is invoked. Create
Advertisements
 

 

 

DMCA.com