Jagadeesh
Location finding error
1 Answer(s)      5 years and 2 months ago
Posted in : JSP-Servlet
Location needs from drive name:

My file uploading program has an error. It requires the location should be given from the directory name. Instead of this if only parent directory is given it shows the error as file not found. Below is the coding.The problem line is marked ^ character.
__________________________________________________
<%@ page import="java.io.*" %>
<%@ include file="../jsp_files/dbcode.jsp"%>
<%@ include file="../jsp_files/security_code.jsp" %>
<%@ include file="../session_files/sessioncheck.jsp" %>
<script src="../scripts/blockrclick.js"></script>
<% int no = (Integer)session.getAttribute("deptno");
String num = String.valueOf(no);
String dno = "03PA"+num;
%>
<%
String ext = null;
int formDataLength=0;
//to get the content type information from JSP Request Header
String contentType = request.getContentType();

//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
if(formDataLength-202>102400){%>
<head>
<meta name="Microsoft Theme" content="sumipntg 011">
</head>

<body background="sumtextb.jpg" bgcolor="#FFFFFF" text="#000066" link="#3333CC" vlink="#666699" alink="#990099">

<!--mstheme--><font face="Verdana, Arial, Helvetica">

<p align="center"><Br>
<div align="center">
<center><!--mstheme--></font><table border="0"><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><b>Your file size exceeded the limit.<br>You have not uploaded your photo</b>
</p>
<!--mstheme--></font>
</td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td><!--mstheme--><font face="Verdana, Arial, Helvetica"><p onclick="window.close()" align="center"><font face="Arial Narrow" size="3" color="#003366"><b><u>Close
Window</u></b></font><!--mstheme--></font></td></tr></table>
<!--mstheme--><font face="Verdana, Arial, Helvetica"> </center>
</div>

<%}else{
try
{
Statement st = con.createStatement();
rs = st.executeQuery("select photo from stud_detail where deptno='"+dno+"'");
}
catch (Exception ex)
{
out.println("Exception"+ex);
}String path=null;
while(rs.next()){path=rs.getString(1);}
File obj = new File("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/mca2006sfssjc.co.in/stud/images/"+path);
boolean ans = obj.delete();
String file = new String(dataBytes);
//for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
/* File name should be given in below savefile variable*/
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//saveFile = "801.txt";

int mid= saveFile.lastIndexOf(".");
ext=saveFile.substring(mid+1,saveFile.length());
String fname = saveFile.substring(0,mid);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
//extracting the index of file
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;

// creating a new file with the same name and writing the content in new file

//";

^^^^^^^^^^^^^^^FileOutputStream fileOut = new FileOutputStream("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/mca2006sfssjc.co.in/stud/images/"+num+"."+ext);^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

%>

<head>
<meta name="Microsoft Theme" content="sumipntg 011">
</head>

<body background="sumtextb.jpg" bgcolor="#FFFFFF" text="#000066" link="#3333CC" vlink="#666699" alink="#990099">

<p align="center"><Br>
<div align="center">
<center><!--mstheme--></font><table border="0"><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><b>You have successfully uploaded your photo</b>
</p>
<!--mstheme--></font>
</td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td>
<!--mstheme--><font face="Verdana, Arial, Helvetica">
<p align="center"><!--mstheme--></font></td></tr><tr><td><!--mstheme--><font face="Verdana, Arial, Helvetica"><p onclick="window.close()" align="center"><font face="Arial Narrow" size="3" color="#003366"><b><u>Close
Window</u></b></font><!--mstheme--></font></td></tr></table>
<!--mstheme--><font face="Verdana, Arial, Helvetica"> </center>
</div>
<!--mstheme--></font>
<%
}}
%>
<%
if(formDataLength-202<102400){
String name = num+"."+ext;

try
{
Statement st = con.createStatement();
int res = st.executeUpdate("update stud_detail set photo='"+name+"' where deptno='"+dno+"'");
}
catch (Exception ex)
{
out.println("Exception"+ex);
}
}%>
_______________________________________________________
Advertisement
View Answers

July 22, 2010 at 4:08 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:
Location finding error - JSP-Servlet
Location finding error  Location needs from drive name: My file uploading program has an error. It requires the location should be given from... the error as file not found. Below is the coding.The problem line is marked
Finding A Date
Finding A Date  My Frequency Start Date is 24-08-2012 My frequency end date - not defined Calculation Frequency is Weekly(Sunday) My calculation will be from 24-08-2012 to the following sunday My question is how to find
Advertisements
Location Reminder
Location Reminder  Hi, i am planing to do a project called location... mark thar place in my mobile, and later if i visit that location,my mobile has to give a reminder. How to mark a location on mobile using GPS? Please give me
cursor location
cursor location   hi,please how to change cursor location by java code ,i use this code but not change the location JTextArea a=new JtextArea(); if(resultText.equalsIgnoreCase("class")){ a.append("a { \n }"); PointerInfo
location - MobileApplications
location  I want to do project in which when i go to college my mobile should get automatically to silent mode using the location where the college... location that come on our mobile display and silent the mobile. im new to J2ME
finding java code on internet
finding java code on internet  Is there anybody to help me? i want to some java coding web sites.it is prerequest for me
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
finding the prime numbers
finding the prime numbers  Hi, I am a beginner to java and I have problem with the code in finding the prime numbers, can someone tell me about the problem. Thanks.   Want to know about how to find out the given
mkmapkit location search
mkmapkit location search  How to create a location search function that will allow the current location search on the MKMapKit
Finding duplicates using array in for loop
Finding duplicates using array in for loop  how to find the duplicates in array using for loop
iphone location framework
iphone location framework  Which framework do i need to add to get the iphone current location in iPhone frameworks?   Add core location to your existing frameworks. CoreLocation.framework   
Finding searching phrase of a search engine
Finding searching phrase of a search engine  how to find out searching phrase of a search engine..? like, if visitors enter the keyword to google, is is any possible to get that keyword
mkmapview zoom to current location
mkmapview zoom to current location  How can i zoom map to the current location
j2me location updates programming
j2me location updates programming  kindly please, assist me with with a j2me code for obtaining and sending GPS location updates periodicaly to the server IE; LATITUDE, LONGITUDE, SPEED, TIME. Am using netbeans IDE and PHP
Remote Location Handeling - RMI
Remote Location Handeling  I have application where I want main control from one location and have to connect two more locations remotly and have... location
Finding page count in any file
Finding page count in any file  Hi, I want to find out the page count in any file (doc,ppt,pdf,image,etc...). Is there any universal API to deal with any file irrespective of the file extension.Its bit urgent. Please let me
Finding smallest value, then the next etc
Finding smallest value, then the next etc  hi, I know how to search for the smallest value index in an unsorted array so far with this: double currentValue = dist[0]; int smallestIndex = 0; for (int j
Location Api - MobileApplications
Location Api  I am using following code to get coordinates of location in gps. On emulator in netbeans it shows me output as 2.0 2.0... a location provider: Criteria cr= new Criteria(); public void
Finding shapes in digital photos - java
Finding shapes in digital photos - java  Hello, I need some hints/tips regarding some project I'm doing. The project is about finding shapes in digital photos (JPEG file). Here's what I have to do: A digital photo
timezone_location_get
timezone_location_get() Function in PHP timezone_location_get alias DateTimeZone::getLocation returns location information for a timezone including country code, latitude/longitude and comments. Syntax of timezone_location_get
Project Location change - Development process
Project Location change  Hi , I have developed mini project . In my machine everything is working fine. But i moved that folder to another machine.... but still am getting 503 error. Is there any possibility to run my project in LAN
Automatic Vehicle Location Advantage,Automatic Vehicle Location,Vehicle Location
Automatic Vehicle Location Advantage     ... Location or AVL is a system primarily used in tracking vehicles, freight... Better security for driver as well as travelers by enabling faster location
JavaScript method location
JavaScript method location         ... using location property of the window object. One of them is location.href which
Finding memory consuming process - Java Beginners
Finding memory consuming process  Hi, While debugging or optimizing your java application, how to find the memory consuming processes? Thanks
Finding all palindrome prime numbers - Java Beginners
Finding all palindrome prime numbers  How do i write a program to Find all palindrome prime numbers between two integers supplied as input (start and end points are excluded
Program in javascript to show the current location
Program in javascript to show the current location  Hi All, Actually in my application ..i wanted to updated the current location each time it gets... location in JavaScript? Thanks
Java Get Class Location
Java Get Class Location   ... location. For this, we have used the classes URL and ClassLoader. The class...(classLocation) returns the location of the class.  import 
i need a java code for finding typemismatch in an editor.
i need a java code for finding typemismatch in an editor.  hi im sriram, i need a java code in such a way that typemismatch code is embedded with the simple java editor. the sample output is shown below !alt text
error
error  while iam compiling iam getting expected error