File and data sending

File and data sending

Can anyone please tell me how to upload a file along with some data into the server??I want to do it like the way they do it in the blogs,where we can upload both file along with some information.I am using multipart/form-data but using this I cannot send the data.So it would be really helpful if someone provides me with the code....

View Answers

December 23, 2011 at 3:45 PM

1)page.jsp:

<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.*,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;
saveFile="C:/UploadedFiles/"+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 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(?)");
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("unsucessfull to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

January 26, 2012 at 8:45 PM

Sorry but this is not exactly what I want.By this code I can only send the file.All I want to do is that there would be a textarea and the user will be able to write something into the textarea.Then if he wants he can include files as well.After he has done these things ,there will be a submit button.Clicking on it will upload the file to the server and the data in the textarea to the database.









Related Tutorials/Questions & Answers:
File and data sending
File and data sending  Can anyone please tell me how to upload a file along with some data into the server??I want to do it like the way they do...(file_data) values(?)"); fis = new FileInputStream(f); psmnt.setBinaryStream(1
sending data to facebook
sending data to facebook  how to post data into facebook from a java program
Advertisements
Sending File - JSP-Servlet
Sending File  How to use tag in Jsp, & How read a file fom Client  Hi Friend, We used tag to upload a file. Please visit the following links: http://www.roseindia.net/jsp/fileupload.shtml http
sending a zip file to servlet
sending a zip file to servlet  I have created a .zip file in a servlet on the local system(no static ip). The .zip file contains xml files. I have to send it to another servlet which is in a server(has a static ip). I have done
Sending data using bluetooth in android
Sending data using bluetooth in android  code to send data between two android mobile devices using bluetooth
sending data from java to facebook wall
sending data from java to facebook wall  how to send data form a java program to facebook wall
Getting and sending data to and from mobile and the system
Getting and sending data to and from mobile and the system  Hi, For getting and sending data to mobile from system.make system is a server.From mobile send a request to the server and get response.Then process the response
sending data to google chart api from mysql database using java
sending data to google chart api from mysql database using java  how to send data from mysql database to google chart api using java
problem in sending data from one ip to another ip in JAVA
problem in sending data from one ip to another ip in JAVA  Hi guys . this is naveen kumar...i need a help. i want to connect one ip to another ip (like ping),after that i want send the data with my system(Ip) to another ip
sending data to google chart api from mysql database using java
sending data to google chart api from mysql database using java  Äîáîâëÿéòåñü â ãðóïïó "ÌÀÉÍÊÐÀÔÒÀ" Áóäüòå ó÷àñòíèêàìè Ãðóïïû Ïîìîãàèòå íàì è ñåáå Íàøà ãðóïïà https://vk.com/club60851464 Åñòü ñâîé ñåðâåð! Õîðîøàÿ àòìîñôåðà
Sending large data to Action Class error. Struts code - Struts
Sending large data to Action Class error. Struts code  I have a jsp page and there is a button which is a image type ,when i click on the button I am..., SrideviMannem) in your D drive. 3. Maintain all your data, documents, projects
Write data in doc file
Write data in doc file  How to write data or string in word file
Insert Data From File
Insert Data From File       Insert Data From File is  used to insert data from file... an example to insert data from file. To understand this example, we create
Insert Data From File
Insert Data From File       Insert Data From File is  used to insert data from file... an example to insert data from file. To understand this example, we create
Data extraction from an image file.
Data extraction from an image file.  I have made an image... data from the the image which also include deleted data.Can i used java to extract these data from the image file? Which are the libraries to be used
Problem with appending data to the end of file
the data(via the browser),the data that i entere is printed on the text file...Problem with appending data to the end of file  MY JSP CODE..." import="java.io.*,data.*" %> <% String name=request.getParameter("name
CONVERT JTable DATA TO PDF FILE
CONVERT JTable DATA TO PDF FILE  HOW TO CONVERT JTable DATA TO .PDF FILE??PLEASE GIVE ME CODE FOR THAT.   Here is an example that reads the jtable data from the jframe and stored the data into the pdf file in the form
Sending form data from HTML page to SQLserver 2005 database by calling servlet code
Sending form data from HTML page to SQLserver 2005 database by calling servlet code  Hi sir, I would like to know how to send the form data from html page to database by calling servlet code from html page .   
Sort file data and write into another file
Sort file data and write into another file By sorting, you can arrange the data into meaningful order and analyze it more effectively. Here we have one file... and grade. The data is not properly arranged in the file. Now, we have to display
Insert file data into database
Insert file data into database In this section, you will learn how to insert the file data into database. Description of code: Here we have used FileReader and BufferedReader class to read the file. The data of the file is then stored
JavaScript get excel file data
JavaScript get excel file data...; By the use of JavaScript we can get the excel file data as well. Here is the example which gets the data from the excel file with the cell and row index
java program to insert data into a file and count the number of words from the file???????
java program to insert data into a file and count the number of words from the file???????  java program to insert data into a file and count the number of words from the file
pass data from java frame to file
pass data from java frame to file  i want to write the data to file, and data must be copy from java frame java frame contains 7-8 labels n below that 1 table. i want store all this data to 1 file. plz help me
ModuleNotFoundError: No module named 'pytest-data-file'
ModuleNotFoundError: No module named 'pytest-data-file'  Hi, My... named 'pytest-data-file' How to remove the ModuleNotFoundError: No module named 'pytest-data-file' error? Thanks   Hi, In your
Explain the relationship among database, tablespace and data file.
Explain the relationship among database, tablespace and data file.  Explain the relationship among database, tablespace and data file.   Hi... spaces are created. 1-System Table Space ADS_TO_REPLACE_2 : This data file
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
Java Binary data file - Java Beginners
Java Binary data file  Hi, I have a binary data file(binfile.data) and the file has what is commonly referred to as variable length records.Specifically the format of the file is: HEADER SECTION//RECORD
Question about "Insert text file data into Database"
Question about "Insert text file data into Database"  Hey I was reading the tutorial "Insert text file data into Database", (awesome btw), and noticed that both a FileInputStream, a DataInputStream and a BufferedReader
Data needs to be gathered in XML file from the database (MySql) using JSP
Data needs to be gathered in XML file from the database (MySql) using JSP ... data regarding particular id from the database table. Data needs to be gathered in XML file from the database (MySql) using appropriate JSP/Java Bean functions
Extract File data into JTable
Extract File data into JTable In this section, you will learn how to read the data from the text file and insert it into JTable. For this, we have created... the BufferedReader class, we have read the data of the file. This data is then broken
how to store data in XML file - JSP-Servlet
how to store data in XML file  hi i have to store the data for example user id and password in a xml file the input userid and password will be coming from jsp middle ware servlet how to do that?   Hi friend
Java insert file data to JTable
Java insert file data to JTable In this section, you will learn how to insert text file data into JTable. Swing has provide useful and sophisticated set...(); frame.setVisible(true); } }  Through the above code, you can insert the file data
Exporting data from mysql to csv file
to export the data from mysql to csv file... i am having 30 columns in my database.. Eg- text1,text2,text3,....,upto text30... i want to export this data in csv file..if i export am getting the csv file with all data in one row.. but i want the csv
read text file and store the data in mysql - JDBC
read text file and store the data in mysql  when we store the data in mysql table from text file its store the data from new line to new column. how to store the data in different column from a single line of text file
Fetching the exact data from file using java
Fetching the exact data from file using java  **hi ... i am having one .lst file.. that file consists of instructions and opcodes.. now i want to fetch only the opcode from that file.. could u pls anyone guide me this using java
how to store data in a text file - Java Beginners
how to store data in a text file  Hi friends, I want to know, how we can save the data in a .txt file using swings....... for example, i want to store the arraylist data of swing program into a text file.......and also i want
retrieving excel data when file uploading
retrieving excel data when file uploading  <%@page import...); } } %> when i gave full path name i got excel data but when file uploading not getting the excel data
Inserting data in Excel File - Java Beginners
Inserting data in Excel File  Dear Sir, How can we fill the excel with fetched data from the database like Oracle, DB2? Is it possible to create an excel with filled data? Kindly give me the solutions to questions
reading data from excel file and plotting graph
reading data from excel file and plotting graph  I am doing a project... the data in excel file, i have to plot graphs based on CELL ID selected. please help... that reads an excel file using POI api and using the data of excel file
is there any possibelities fast read and write file large data file
is there any possibelities fast read and write file large data file  ...{ String sCurrentLine; // Groped file name (Suresh.txt...(Exception ex){ //File file = new File("D:\\groupby.txt
is there any possibelities fast read and write file large data file
is there any possibelities fast read and write file large data file  ...{ String sCurrentLine; // Groped file name (Suresh.txt...(Exception ex){ //File file = new File("D:\\groupby.txt
is there any possibelities fast read and write file large data file
is there any possibelities fast read and write file large data file  ...{ String sCurrentLine; // Groped file name (Suresh.txt...(Exception ex){ //File file = new File("D:\\groupby.txt
Delete and edit data in xml file using JSP
Delete and edit data in xml file using JSP   I want to know how to delete and edit data from an XML file by use of JSP. I have XML file having tasks... in the xml file,I want to delete and edit some tasks using task id then how can i do
convert excel file data into different locales while converting exclile file to csv file
convert excel file data into different locales while converting exclile file to csv file   can any one provide the code for how to convert excel file data into different locales while converting exclile file to csv file
how to display data from jsp file into database
how to display data from jsp file into database  this is a jsp file...+","+ph2+")"); out.println("Data is successfully inserted...+",'"+email+"')"); out.println("Data is successfully inserted into database
connection of java file to excel file and put data from excel file into oracle db
connection of java file to excel file and put data from excel file into oracle... give action to that button excel sheet? how we connect excel file to java file as wellas to oracle db to insert excel data into database
sending mail - JSP-Servlet
sending mail  Hi, what is the code for sending mail automatically without user intervention? thanks in advance
sending mails - JSP-Servlet
sending mails  my intension is sending mails through java program with out installing any softwares.what is code

Ads