BROWSE BUTTON CODE

Ads
 

BROWSE BUTTON CODE

HOW TO STORE THE FILE BROWSE TO THE DATABASE

View Answers

February 23, 2011 at 11:18 AM

JSP File Upload

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.*" %>
<%@ 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;
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();}
}
%>

Ads









Related Tutorials/Questions & Answers:
BROWSE BUTTON CODE
BROWSE BUTTON CODE  HOW TO STORE THE FILE BROWSE TO THE DATABASE   JSP File Upload 1)page.jsp: <%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form to the user</TITLE>
BROWSE BUTTON CODE
BROWSE BUTTON CODE  HOW TO STORE THE FILE BROWSE TO THE DATABASE   JSP File Upload 1)page.jsp: <%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form to the user</TITLE>
Advertisements
Export data in excel sheet via Browse and upload button into mysql database
Export data in excel sheet via Browse and upload button into mysql database  how to create a Browse & Upload Buttons and then save the information from it in mysql database's Table Am using struts2,hibernate for making
facebook like button code
facebook like button code  How to create a facebook like button? can anyone share the code..?? Thanks in advance
code for insert button
code for insert button    i want code for insert button(in image...;The given code uses Swing components to create a image button and allow the user to enter name and address. As the user clicks the image button, the data will get
How to set attachment from browse button to send mails using java mail api
How to set attachment from browse button to send mails using java mail api   Hello Sir, I am making Email Sending app using java mail api... browse button but attachment is not loading properly using browse button because
browse image
browse image  how to browse the image in image box by browse button and save image in database by save button by swing   import java.sql.... img; JTextField text=new JTextField(20); JButton browse,save; JPanel p=new
browse image
browse image  how to browse the image in image box by browse button and save image in database by save button by swing   import java.sql.... img; JTextField text=new JTextField(20); JButton browse,save; JPanel p=new
in the below code two errors are there one at the button onclick atttibute and the next is at the next button
in the below code two errors are there one at the button onclick atttibute and the next is at the next button  <%@ page import="java.sql.*" %> function editRecord(id){ var f=document.form; f.method="post
Browse an image
Browse an image  hi................ i want to browse an image from... text=new JTextField(20); JButton browse,save; JPanel p=new JPanel(new... UploadImage() { browse = new JButton("Upload"); save = new JButton("Save
button
button  can i give multiple commands on click of a button? if yes how can i do that?? multiple commands can we retriving dat from database, capturing data, moving to next page.. etc
button
button  can i give multiple commands on click of a button? if yes how can i do that?? multiple commands can be retriving dat from database, capturing data, moving to next page.. etc
browse and submit the excel sheet into mysql as a table
browse and submit the excel sheet into mysql as a table  sir, i want to browse and after clicking a submit button, the data in the excel sheet should be stored in the mysql5.0 as a table
ModuleNotFoundError: No module named 'browse'
ModuleNotFoundError: No module named 'browse'  Hi, My Python... 'browse' How to remove the ModuleNotFoundError: No module named 'browse'... to install padas library. You can install browse python with following command
ModuleNotFoundError: No module named 'browse'
ModuleNotFoundError: No module named 'browse'  Hi, My Python... 'browse' How to remove the ModuleNotFoundError: No module named 'browse'... to install padas library. You can install browse python with following command
ModuleNotFoundError: No module named 'git-browse'
ModuleNotFoundError: No module named 'git-browse'  Hi, My Python... 'git-browse' How to remove the ModuleNotFoundError: No module named 'git-browse' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'git-browse'
ModuleNotFoundError: No module named 'git-browse'  Hi, My Python... 'git-browse' How to remove the ModuleNotFoundError: No module named 'git-browse' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'git-browse'
ModuleNotFoundError: No module named 'git-browse'  Hi, My Python... 'git-browse' How to remove the ModuleNotFoundError: No module named 'git-browse' error? Thanks   Hi, In your python environment you
how to browse folder and directory in jsp
how to browse folder and directory in jsp  how to browse folder and directory in my jsp page.i have done my coding part for browsing directory but dont know how to get files after that.and also i want to show limited directories
coding of button
coding of button  What is code of fetching the value of button in a textbox using javascript
Print Button
Print Button  i want the source code for printing details of jform.. so plz help me for source code of print button
ModuleNotFoundError: No module named 'npm-browse-cli'
ModuleNotFoundError: No module named 'npm-browse-cli'  Hi, My... named 'npm-browse-cli' How to remove the ModuleNotFoundError: No module named 'npm-browse-cli' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'npmjs-browse-cli'
ModuleNotFoundError: No module named 'npmjs-browse-cli'  Hi, My... named 'npmjs-browse-cli' How to remove the ModuleNotFoundError: No module named 'npmjs-browse-cli' error? Thanks   Hi, In your
Maven Dependency box-android-browse-sdk >> 1.1.0
You should include the dependency code given in this page to add Maven Dependency of com.box >> box-android-browse-sdk version1.1.0 in your project
Maven Dependency box-android-browse-sdk >> 1.1.1
You should include the dependency code given in this page to add Maven Dependency of com.box >> box-android-browse-sdk version1.1.1 in your project
Maven Dependency box-android-browse-sdk >> 1.1.2
You should include the dependency code given in this page to add Maven Dependency of com.box >> box-android-browse-sdk version1.1.2 in your project
Maven Dependency box-android-browse-sdk >> 2.0.0
You should include the dependency code given in this page to add Maven Dependency of com.box >> box-android-browse-sdk version2.0.0 in your project
Maven Dependency box-android-browse-sdk >> 2.0.1
You should include the dependency code given in this page to add Maven Dependency of com.box >> box-android-browse-sdk version2.0.1 in your project
uikeyboard done button/return button
uikeyboard done button/return button  Hi everyone!!! Just worried about ..how to return the UIKeyboard on done or return button click in my iPhone application. Thanks   Hi, Find the given code that will allow you
Version of com.box>box-android-browse-sdk dependency
List of Version of com.box>box-android-browse-sdk dependency
how to use button in array
how to use button in array  plz tell me how to code for button in array

Ads