Changing loacation for file object

Changing loacation for file object

Hai,

For uploading a file i used the FileOutputStream() method. And uploading works perfectly. This method allows parameter as file name or the full path(that is from the drive letter. For example-->"C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/mca2006sfssjc.co.in/stud/images/"+num+"."+ext) is required. Giving full path will not work in another machine. And if i give the file name only it stores or uploads the file to the folder C:\Program Files\Apache Software Foundation\Tomcat 6.0. Now, i ned a method to specify a location to store the file which is uploaded. I required to store the file at
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\mca2006sfssjc.co.in\stud\images by changing the location to the folder images instead of giving this full path.

Thanks.
View Answers

July 23, 2010 at 12:52 PM

Hi Friend,

Try the following code:

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=
"uploadandstore.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 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;

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(?)");
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();}
}
%>


Thanks









Related Tutorials/Questions & Answers:
Changing loacation for file object - JSP-Servlet
Changing loacation for file object  Hai, For uploading a file i used the FileOutputStream() method. And uploading works perfectly. This method allows parameter as file name or the full path(that is from the drive
changing the file content
changing the file content  I have requirement like this, a file... , reading the file content and changeing the 501 line content and crating one more file and storing this changeing information String line=null; String
Advertisements
Changing Executable Jar File Icon
Changing Executable Jar File Icon  I have created an executable jar file for my java program and the icon that appears is the java icon. I will like...;Hi, You may use JSmooth to create executable java file and also associate icon
Java file object
Java file object This section demonstrates you the concept of File object... the programming much easier. Here we will discus the operations performed by File object. Using the File object, you can perform several operations. It parses
ModuleNotFoundError: No module named 'nxpy-file-object'
ModuleNotFoundError: No module named 'nxpy-file-object'  Hi, My... named 'nxpy-file-object' How to remove the ModuleNotFoundError: No module named 'nxpy-file-object' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'py-object-file'
ModuleNotFoundError: No module named 'py-object-file'  Hi, My... named 'py-object-file' How to remove the ModuleNotFoundError: No module named 'py-object-file' error? Thanks   Hi, In your python
loacation of errorpage in web.xml for 404 error
loacation of errorpage in web.xml for 404 error   The webpage cannot be found HTTP 404 Most likely causes: ā?¢There might be a typing error in the address. ā?¢If you clicked on a link, it may be out of date. What you
parsing XML file to get java object - XML
parsing XML file to get java object  Hello, I'm facing a problem in parsing XML file to get the java object. I've tried to retrieve data from XML file using SAX parser. my XML file structure is the following
changing of data...
servlet.database is not changing in 10 second  hi.... i have created a servlet page using a response.setheader(); method.in the parameter i have... this servlet.database is not changing in 10 second...what should i do..?? reply fast
changing of data...
data changing in 10 second  hi.... i have created a servlet page using a response.setheader(); method.in the parameter i have given 10 second.but... is not changing in 10 second...what should i do..?? reply fast
changing of data...
changing of data...  hi.... i have created a servlet page using a response.setheader(); method.in the parameter i have given 10 second.but once... is not changing in 10 second...what should i do..?? reply fast
how to add two object in a particular file - Java Beginners
how to add two object in a particular file  Hi frend.. I have two arraylist object in which there is some data..............now i want to add these two objects in a particular file using file handling in java....and also
Maven Repository/Dependency: org.qbicc | qbicc-machine-file-object
Maven Repository/Dependency of Group ID org.qbicc and Artifact ID qbicc-machine-file-object. Latest version of org.qbicc:qbicc-machine-file-object dependencies. # Version Release Date
object
object  is it possible to create object in the same class..?.   Yes, you can. class CreateObject { CreateObject(){ System.out.println("I have an object."); } public static void main(String[] args
Changing pictures - Java Beginners
Changing pictures  I found some html code that allows pictures to be changed on a web page. I copied the code and saved it as an html file. when I... Explorer that have pictures changing on them and they do not get blocked? 
changing uiview background color
changing uiview background color  changing UIView background color in ios   self.view.backgroundColor = [UIColor colorWithRed:244.0f/255.0f green:230.0f/255.0f blue:183.0f/255.0f alpha:1.0f
Changing MySQL Port Number
Changing MySQL Port Number  How to change the MySQL port no in windows.   Hi, Open the my.ini file present in the MySQL server... to the desired port number. After making change in my.ini file save the file
Changing root Password
Changing root Password  how do i change the root password in mysql
Changing 2D graph using slider.
Changing 2D graph using slider.  Code for changing a 2d graph with help of slider
changing Background Color
changing Background Color   ... change background color of a pdf file. You can change the background color... the object of Color by using new Color(0xFF, 0xFF, 0x00). We are passing three
changing selection color of <button>
changing selection color of   dear all, i have loaded image in border less button tag when button is selected with tab key i get brown color rectangle around image. how do i change color of that rectangle from brown to white
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.55.0 is released. Learn to use qbicc-machine-file-object version 0.55.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 14 Dec 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.52.0 is released. Learn to use qbicc-machine-file-object version 0.52.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 29 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.51.0 is released. Learn to use qbicc-machine-file-object version 0.51.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 17 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.50.0 is released. Learn to use qbicc-machine-file-object version 0.50.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 16 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.52.0 is released. Learn to use qbicc-machine-file-object version 0.52.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 29 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.51.0 is released. Learn to use qbicc-machine-file-object version 0.51.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 17 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.50.0 is released. Learn to use qbicc-machine-file-object version 0.50.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 16 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.49.0 is released. Learn to use qbicc-machine-file-object version 0.49.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 11 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.54.0 is released. Learn to use qbicc-machine-file-object version 0.54.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 10 Dec 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.53.0 is released. Learn to use qbicc-machine-file-object version 0.53.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 06 Dec 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.56.0 is released. Learn to use qbicc-machine-file-object version 0.56.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 16 Dec 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.57.0 is released. Learn to use qbicc-machine-file-object version 0.57.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 19 Dec 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.48.0 is released. Learn to use qbicc-machine-file-object version 0.48.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 01 Nov 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.47.0 is released. Learn to use qbicc-machine-file-object version 0.47.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 20 Oct 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.46.0 is released. Learn to use qbicc-machine-file-object version 0.46.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 14 Oct 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.45.0 is released. Learn to use qbicc-machine-file-object version 0.45.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 28 Sep 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.44.0 is released. Learn to use qbicc-machine-file-object version 0.44.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 22 Sep 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.43.0 is released. Learn to use qbicc-machine-file-object version 0.43.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 07 Sep 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.42.0 is released. Learn to use qbicc-machine-file-object version 0.42.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 02 Sep 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.41.0 is released. Learn to use qbicc-machine-file-object version 0.41.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 29 Aug 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.40.0 is released. Learn to use qbicc-machine-file-object version 0.40.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 28 Jul 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.39.0 is released. Learn to use qbicc-machine-file-object version 0.39.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 18 Jul 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.38.0 is released. Learn to use qbicc-machine-file-object version 0.38.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 06 Jul 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.37.0 is released. Learn to use qbicc-machine-file-object version 0.37.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 01 Jul 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.36.0 is released. Learn to use qbicc-machine-file-object version 0.36.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 24 Jun 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.35.0 is released. Learn to use qbicc-machine-file-object version 0.35.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 17 Jun 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.34.0 is released. Learn to use qbicc-machine-file-object version 0.34.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file-object project have released the latest version of this library on 08 Jun 2022, the released version of  org.qbicc - qbicc-machine-file-object library
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.6.0 is released. Learn to use qbicc-machine-file-object version 0.6.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file..., the released version of  org.qbicc - qbicc-machine-file-object library is 0.6.0. Developer can use this version ( org.qbicc - qbicc-machine-file-object
Maven dependency for org.qbicc - qbicc-machine-file-object version 0.5.0 is released. Learn to use qbicc-machine-file-object version 0.5.0 in Maven based Java projects
-file-object released The developers of   org.qbicc - qbicc-machine-file..., the released version of  org.qbicc - qbicc-machine-file-object library is 0.5.0. Developer can use this version ( org.qbicc - qbicc-machine-file-object

Ads