Reputation: 985
hi i am uploading images in to data base using servlets
. when i submit my form i am getting a exception "NULL" here is my code any help are accepted.And can any one suggest me which way to be used to upload files in to database such that there wont be much of load on database and lessen httprequest
. thank you in advance.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="requirementlogic.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<body>
<form method="post" action="../InsertImage">
<table>
<tr><TD ><B>Upload Image</B></TD>
<td><input type="file" name="Image" size="20" value=""></TD>
</tr>
<tr>
<td><input type="submit" height="30" width="62"> </td>
</tr>
<tr>
</table>
</form>
</body>
</html>
here is my JDBC:
package requirementlogic;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DataBase {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbname= "deshopa";
String driver ="com.mysql.jdbc.Driver";
String Username="root";
String Password ="";
public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+dbname,Username,Password);
return con;
}
}
here is my servlet:
package requirementlogic;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class InsertImage
*/
@WebServlet("/InsertImage")
public class InsertImage extends HttpServlet {
private static final long serialVersionUID = 1L;
DataBase db;
/**
* @see HttpServlet#HttpServlet()
*/
public InsertImage() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
db = new DataBase();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
try
{
String strpath=request.getParameter("image");
String filepath=strpath.substring(strpath.lastIndexOf(".")+1);
pw.println(filepath);
File imgfile = new File(strpath);
FileInputStream fin = new FileInputStream(imgfile);
String sql = "insert into upload_image(image,image_name,image_length)";
PreparedStatement pre = db.getConnection().prepareStatement(sql);
pre.setBinaryStream(1,fin,(int)imgfile.length());
pre.setString(2,filepath);
pre.setLong(3,imgfile.length());
pre.executeUpdate();
pre.close();
pw.println("done");
}
catch(Exception e){
pw.println("direct exception");
pw.println("Exception is "+ e.getMessage());
}
}
}
Sql query used to create table:
CREATE TABLE `upload_image` (
`Image_id` int(11) NOT NULL AUTO_INCREMENT ,
`IMAGE` longblob NULL DEFAULT NULL ,
`Image_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`image_length` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`Image_id`)
)
Upvotes: 0
Views: 2190
Reputation: 6965
You are trying this:
String strpath=request.getParameter("image");
Try this:
String strpath=request.getParameter("Image");
Error is name which you are trying. Name in capital letter.
<input type="file" name="Image" size="20" value="">
In case
File file = new File("yourPath");
file.mkdirs();
You can try this Link1 and Link2
Upvotes: 0
Reputation: 2895
change to this
String strpath=request.getParameter("Image");
as in your html form the name of the file field is Image
not image
<td><input type="file" name="Image" size="20" value=""></TD>
also you are dealing with multipart request
so you have to chenge your code
if(ServletFileUpload.isMultipartContent(request)){
private final String UPLOAD_DIRECTORY = "C:/uploads";
try {
List<FileItem> multiparts = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
}
}
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
}
}
See the below link for further details
Upvotes: 1