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



Home Answers Viewqa JSP-Servlet which path we have to give in order to upload a file from any where in the system

Have Programming Question? Ask it here!
 
 
 


Dharani
which path we have to give in order to upload a file from any where in the system
1 Answer(s)      2 years and 7 months ago
Posted in : JSP-Servlet


When i am uploading an image to MySQL database in this i am to specifying a path in the code from that path only it is taking image . what i have to do in order to upload a image from any where from the system Please tell me quickly i need it


Advertisement
View Answers

February 6, 2012 at 11:45 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.*" %>
<%@ 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();}
}
%>


Related Tutorials/Questions & Answers:
which path we have to give in order to upload a file from any where in the system
which path we have to give in order to upload a file from any where... to specifying a path in the code from that path only it is taking image . what i have to do in order to upload a image from any where from the system Please tell me
need solution to get file name from path which should give by user
need solution to get file name from path which should give by user  how do i write a program in java that i should get file path from arguments.currently this code creates the file at d:\ but i need to get this file name
Advertisements
Web Service - Which files we have to give to client and how they will use that
Web Service - Which files we have to give to client and how they will use... this web service or want to give it to client , for that which files i have to give... and have to use it, how we call this in jsp/servlet 3) Which type of response we
which values we have to use for compressing any color image
which values we have to use for compressing any color image  Sir........... in the process of compressing of any color image, which values do use as input for comprseeing algorithm. and how to get the image matrix from color
about Detail file path where to store application.
about Detail file path where to store application.  can you tell me detail path where we have to store our application
executing a batch file which depends on jar files from a system tray but console should not display.
executing a batch file which depends on jar files from a system tray... to run a batch file which is depending on some jar files and it displays a frame and some output generated by System.out.println()'s from system tray. I want
Java Get File Name
Java Get File Name       In this section, you will study how to obtain the name of file.  In order to get the file name, we have provided the path of file 'Hello.txt
How to give path to the Dfile?
on sphinx, i have to give path to the sphinx4.jar file which is present in my...How to give path to the Dfile?  Hello erveryone, I want to make... you give me the write path, please help me
how to upload multiple files in jsp and saving the path in database and the file in folder
how to upload multiple files in jsp and saving the path in database and the file in folder  how to upload multiple files in jsp and saving the path in database and the file in folder I have created a form for the upload of files
Storing content from file path to an array
Storing content from file path to an array  Hi, I have a path... would like to access the "Animation.txt" file from this path and store the contents in this text file to an array. I am using jsp. I can access my path but how
in unix which command is used to sort the lines of data in a file in reverse order
in unix which command is used to sort the lines of data in a file in reverse order  in unix which command is used to sort the lines of data in a file... the lines of data in a file in reverse order
File Upload Tutorial With Examples In JSP
in the developing the project in which you have to upload any type of files... File Upload Tutorial With Examples In JSP  ... by Using JSP This tutorial will help you to understand how you can upload a file
why and where we use setter and getter methods in java, please give me one example program.....
why and where we use setter and getter methods in java, please give me one example program.....  why and where we use setter and getter methods in java, please give me one example program
Write a method which will remove any given character from a string?
Write a method which will remove any given character from a string? In this example we will describe remove any given character from a string. In this tutorial we have used StringBuilder is a mutable sequence of character. It is used
Struts 2 File Upload
Example code to upload file Now we will write the code to upload the file... Struts 2 File Upload       In this section you will learn how to write program in Struts 2 to upload the file
Fileupload from source path to destination path
Fileupload from source path to destination path  first we will create one .doc file and .rtf file in one location statically.In html we will give... will give source path as statically where the .doc or .rtf files path will be their.and
Java Construct File Path
; (separator character). But, we can construct a file path system independent due...Java Construct File Path In this section we will discuss about how.... When you will construct a file path manually the path name will be system
Order Entry System
Order Entry System  Create a new order entry system using netbeans and microsoft access.can anyone help me
Java example to get the execution path
;     get execution path We can get the execution path of the system in java by using the system property. For getting execution path we have used "user.dir" property of the system. Here
How and Where we Access struts Properties file - Struts
How and Where we Access struts Properties file   Hi Friends, am new...) and for future enhancements we just call properties file . Now i want to implement this , where can i call properties file to enhance my application. My question
SWT File Browser
, the class FileDialog allow the user to navigate the file system and select a file name. In order to layout the text, we have used the method text.setLayoutData...;null) {   File file = new File(path);  
File Upload
File Upload  when i execute the image upload to mysql database it shows an exception that file path cannot be found
File path for jar file
File path for jar file  Hi Experts, I have created one eclipse... path of the template, which is working as per expectation. But I have created... not found for the template file. Also I had tried to get that path from user
Java program to get the desktop Path
;     In this example program we have to get the desktop path of the system. In the java environment we can get the desktop path also with the system's property. For getting the desktop path we have to add
We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only
We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only  We just have one file on the hard disk. But we can create directories, sub-directories and files in that file
We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only
We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only  We just have one file on the hard disk. But we can create directories, sub-directories and files in that file
How to upload zip file from android to server programmatically?????
How to upload zip file from android to server programmatically?????  hi , I want to upload zip file from android phone sdcard to server programmatically. So any one do this task in past so please share your experience with me
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload  Dear Sir, My issue is: How display a image on servlet from file upload I receive your answer today... is not running mode which you give me. I am using weblogic 8.1 have jdk 1.4. I
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text... the JButton. Each line in the file then needs to be converted in Reverse Order... to display the names and path on the console. But I can not get the file to read line
any body have any solutions?
any body have any solutions?  Hi Friends, I just get down my computer, I am working on it, but suddenly get down and can√Ę‚?¨‚?Ęt get start again. If any body have any solutions
How to play only video file from any link
How to play only video file from any link  Hi Friends Please Help Me. My requirement is to play only videos from any webpages(ITS NOT A ABSOLUTE PATH)[IF I PASTE any VRL---,IT HAS TO BE PLAY IN ANY CONTROL] how it possible
how to upload a file of apk format in a folder from servlet
how to upload a file of apk format in a folder from servlet  How to upload a file of apk format to a folder from servlet, i am trying to do, its getting uploaded as .zip file and not apk file
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload   Dear Sir, My Question is: How display a image on servlet from file upload Your Answer: Hi... of file. like any .jpg, .txt. Servlet page dispaly empty in each cases
Given a list of responsibilities, identify which belong to the deployer, bean provider, application assembler, container provider, system administrator, or any combination.
, application assembler, container provider, system administrator, or any... the ejb-name of the target bean separated from the path name... Given a list of responsibilities, identify which belong
How to include Two multi rows, one with a file Upload and another with normal fields, have problem while including both
How to include Two multi rows, one with a file Upload and another with normal fields, have problem while including both  How to include Two multi rows, one with a file Upload and another with normal fields, have problem while
Java file absolute path
the root element and the complete directory list to locate the file. Here we have... in the constructor of File class whose absolute path we want to know. Then call...() method, you can find out the absolute path of any file
How display a Image on servlet from file upload - JSP-Servlet
How display a Image on servlet from file upload  Dear Sir, I were ask a question that How display the Image on servlet through file upload. Today I... -------------------------------------------- If you have any problem then explain
Multiple file upload - Struts
Multiple file upload  HI all, I m trying to upload multiple files... need to upload is dynamically generated. I have tried using formfile. and also..." where dataDisp is a seperate bean whose object we are placing in an array
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
where this file is stored
where this file is stored  thank your sir, but where this file is stored? and how to get the path for further use
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
From a list of responsibilities, identify which belong to the application assembler, bean provider, deployer, container provider, or system administrator.
perform different roles. From a security point of view, we can say.... The system administrator tells the application server where to find... From a list of responsibilities
where to stroe this uploaded file
where to stroe this uploaded file  thank your sir, but where this file is stored? and how to get the path for further use
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 a file - Framework
upload a file  Hi to All, I need to upload a file from the client and that upload file should be sent to our co., mail please if any one provide.... Thankyou in Advance.  Hi friend, For upload a file in JSP visit
Java get Absolute Path
of a particular file. For this, we have define a file 'MainClass.java'. The method file.getAbsolutePath() returns the absolute path of the given file.    ... Java get Absolute Path     
file upload download - Java Beginners
file upload download  how to upload and download files from one system to another using java.io.* and java.net.* only please send me code
 

 

 

DMCA.com