Dharani
which path we have to give in order to upload a file from any where in the system
1 Answer(s)      3 years and 3 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
how to show image as a link which path coming from database
how to show image as a link which path coming from database   iam not getting proper answer for it. I am using netbeans .the url coming instring from database ,want to display as image on jsp . please help me
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
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
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 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
Order Entry System
Order Entry System  Create a new order entry system using netbeans and microsoft access.can anyone help me
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 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
File Upload
File Upload  when i execute the image upload to mysql database it shows an exception that file path cannot be found
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
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 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