Thứ Ba, 2 tháng 9, 2014

Uploading images to server using simple java code.

Before starting your java code create table in your local database server

CREATE TABLE `uploadimage` (   `id` int(11) DEFAULT NULL,   `image` longblob );

Now write java code to upload your image

java code:
-----------
import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class uploadServlet {
    public static void main(String []arg)
    {
    Connection con=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    String sql="insert into uploadImage values(?,?)";
    File f=null;
    FileInputStream fin=null;
        try
        {
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/shasheikumar","root","shashei");
        pst=con.prepareStatement(sql);
        pst.setInt(1,2);
        f=new File("C:/wamp/1507517_561872823910921_8191424257028198921_o.jpg");
        fin=new FileInputStream(f);
        pst.setBinaryStream(2,(InputStream)fin,(int)f.length());
        int res=pst.executeUpdate();
            if(res>0)
            {
                System.out.print("your data uploaded");
            }
            else
                System.out.print("an error in uploading data");
        }
       
        catch(Exception e) {
            // TODO: handle exception
            System.out.print(e.getLocalizedMessage());
        }
       
                  

    }
   
}
output:

--------------

your data uploaded


checking weather my data uploaded to server or not

open your server command prompt.type

select * from uploadImage;

Now Right click on BLOB in the bottom you will get



Không có nhận xét nào:

Đăng nhận xét