•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,036 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,284 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 325 | Replies: 0
![]() |
•
•
Join Date: Jan 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
hi ..problem in uploading image..using servlet to upload image in mysql..use html form so user given the path of image..but giving error.here is the code..help me on this.
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoadImage extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
entrance(req, res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
entrance(req, res);
}
public void entrance(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String photo=req.getParameter("photo");
Connection conn = null;
PreparedStatement ps = null;
try
{
Class.forName( "com.mysql.jdbc.Driver" );
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/first","root","root");
ps = conn.prepareStatement("INSERT INTO uplo (photo) VALUES( ? )" );
File image = new File( photo );
FileInputStream fis = new FileInputStream( image );
ps.setBinaryStream(1, fis, ( int )image.length() );
// Execute the INSERT
int count = ps.executeUpdate();
System.out.println( "Rows inserted: " + count );
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
try
{
if( ps != null ) ps.close();
if( conn != null ) conn.close();
}
catch( Exception ee )
{
ee.printStackTrace();
}
}
}
}
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoadImage extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
entrance(req, res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
entrance(req, res);
}
public void entrance(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String photo=req.getParameter("photo");
Connection conn = null;
PreparedStatement ps = null;
try
{
Class.forName( "com.mysql.jdbc.Driver" );
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/first","root","root");
ps = conn.prepareStatement("INSERT INTO uplo (photo) VALUES( ? )" );
File image = new File( photo );
FileInputStream fis = new FileInputStream( image );
ps.setBinaryStream(1, fis, ( int )image.length() );
// Execute the INSERT
int count = ps.executeUpdate();
System.out.println( "Rows inserted: " + count );
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
try
{
if( ps != null ) ps.close();
if( conn != null ) conn.close();
}
catch( Exception ee )
{
ee.printStackTrace();
}
}
}
}
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- image upload problem (PHP)
- Image Uploading (ASP)
- Image not getting refreshed automatically (PHP)
- uploading Images into Database (ASP.NET)
- File upload problem (PHP)
- problem uploading org chart with live link to intranet (Software Development Job Offers)
- check for images duplication (ASP.NET)
- Internet graphics display problem (OS X)
Other Threads in the Java Forum
- Previous Thread: Capitalize problem
- Next Thread: what is return 0 or return 1 ?


Linear Mode