i have made a web application in which you can upload a file and i have used File image = new File(image); here String image = request.getParameter("file"); and file is the name of FILESELECT button or BROWSE button . and i am expecting to obtain the complete path of the image from FILE that i have browsed but instead i m gettng just the name of the image.
this is my index.jsp page
<p><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"></p>
<p><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<FORM ACTION="upload" METHOD=POST>
<br><br><br>
<center><table border="2" >
<tr>
<center>
<td colspan="2"><p align="center">
<B>UPLOAD THE FILE</B>
</td>
</center>
</tr>
<tr>
<td>
<b>Choose the file To Upload:</b>
</td>
<td>
<INPUT NAME="file" TYPE="file" value="">
</td>
</tr>
<tr>
<td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p>
</td>
</tr>
</table>
</center>
</FORM>
</body>
</html></p>
<p>and this is my servlet:upload.java
package controller;</p>
<p>import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;</p>
<p>public class upload extends HttpServlet {</p>
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
String imageUrl = request.getParameter("file");
Connection connection = null;
String connectionURL = "jdbc:mysql://127.0.0.1:3306/skill_tracker";
ResultSet rs = null;
PreparedStatement psmnt = null;
// declare FileInputStream object to store binary stream of given image.
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
// create a file object for image by specifying full path of image as parameter.
File image = new File(imageUrl);
out.println(image);
FileInputStream fis = new FileInputStream(image);
psmnt = connection.prepareStatement("insert into pic values(?,?)");
psmnt.setInt(1,'1');
psmnt.setBinaryStream (2, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query
insert data and image from specified address. */
int s = psmnt.executeUpdate();
if(s>0) {
out.println("Uploaded successfully !");
}
else {
out.println("unsucessfull to upload image.");
}
}
catch (Exception ex)
{
out.println("Found some error : "+ex);
}
}
<p>}</p>
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="../UploadServlet" 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)UploadServlet.java:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
PrintWriter out = response.getWriter();
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);
String 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();
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) {
out.println("Uploaded successfully !");
}
else{
out.println("unsucessfull to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
}
}
Thanks
dat is very difficulto understand wat i have done is very simple...my code works on some system perfectly but on my system it has dis problm..if u can plz tell me waT can i do to get the complete path??
Is it possible to do the same jsp function with php??? just curious...:-)