为了账号安全,请及时绑定邮箱和手机立即绑定

怎样将图片上传到数据库?

mysql数据库可以上传保存文件的吗?、

正在回答

4 回答

public class Ckb_test {  
  
    public static void main(String[] args) throws SQLException, IOException {  
        // TODO Auto-generated method stub  
        read();  
    }  
    static void create() throws SQLException, IOException  
    {  
        Connection conn=null;  
        PreparedStatement prest=null;  
        ResultSet resultset=null;  
          
        try {  
            //2.建立连接  
            conn=JdbcUtils.getConnection();  
            //单例设计模式  
            conn=JdbcUtilsSingle.getInstance().getConnection();  
            //3.创建语句  
            String sql="insert into ckb_test(text) values(?)";  
            prest=conn.prepareStatement(sql);  
            File file=new File("src/cn/com/JDBC/JdbcUtils.java");  
            Reader reader=new BufferedReader(new FileReader(file));  
            prest.setCharacterStream(1, reader, (int)file.length());  
            //4.执行语句  
              
            int i=prest.executeUpdate();  
            reader.close();  
            System.out.println("i="+i);  
        } finally  
        {  
            JdbcUtils.free(resultset, prest, conn);  
        }  
    }  
    static void read() throws SQLException, IOException  
    {  
        Connection conn=null;  
        Statement st=null;  
        ResultSet resultset=null;  
          
        try {  
            //2.建立连接  
            conn=JdbcUtils.getConnection();  
            //单例设计模式  
            conn=JdbcUtilsSingle.getInstance().getConnection();  
            //3.创建语句  
            st=conn.createStatement();  
            //4.执行语句  
            resultset=st.executeQuery("select text from ckb_test");  
            //5.处理结果  
            while(resultset.next())  
            {  
                Clob clob=resultset.getClob(1);  
                Reader reader=clob.getCharacterStream();  
                //reader=resultset.getCharacterStream(1);  
                File file=new File("JdbcUtils.java");  
                Writer writer=new BufferedWriter(new FileWriter(file));  
                char[] buff=new char[1024];  
                for(int i=0;(i=reader.read(buff))>0;)  
                {  
                    writer.write(buff,0,i);  
                }  
                writer.close();  
                reader.close();  
            }  
              
        } finally  
        {  
            JdbcUtils.free(resultset, st, conn);  
        }  
    }  
  
}  
  
----------------------------------------------------------------------------------------------------------  

public class PictureBlob {  
  
    public static void main(String[] args) throws SQLException, IOException {  
        read();  
    }  
    static void create() throws SQLException, IOException  
    {  
        Connection conn=null;  
        PreparedStatement prest=null;  
        ResultSet resultset=null;  
          
        try {  
            //2.建立连接  
            conn=JdbcUtils.getConnection();  
            //单例设计模式  
            conn=JdbcUtilsSingle.getInstance().getConnection();  
            //3.创建语句  
            String sql="insert into blob_test(big_bit) values(?)";  
            prest=conn.prepareStatement(sql);  
            File file=new File("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cxg.jpg");  
            InputStream in=new BufferedInputStream(new FileInputStream(file));  
            prest.setBinaryStream(1, in, (int)file.length());  
            //4.执行语句  
              
            int i=prest.executeUpdate();  
            in.close();  
            System.out.println("i="+i);  
        } finally  
        {  
            JdbcUtils.free(resultset, prest, conn);  
        }  
          
    }  
      
    static void read() throws SQLException, IOException  
    {  
        Connection conn=null;  
        Statement st=null;  
        ResultSet resultset=null;  
          
        try {  
            //2.建立连接  
            conn=JdbcUtils.getConnection();  
            //单例设计模式  
            conn=JdbcUtilsSingle.getInstance().getConnection();  
            //3.创建语句  
            st=conn.createStatement();  
            //4.执行语句  
            resultset=st.executeQuery("select big_bit from blob_test");  
            //5.处理结果  
            while(resultset.next())  
            {  
                Blob blob=resultset.getBlob(1);  
                InputStream in=blob.getBinaryStream();  
                //reader=resultset.getCharacterStream(1);  
                File file=new File("1.jpeg");  
                OutputStream out=new BufferedOutputStream(new FileOutputStream(file));  
                byte[] buff=new byte[1024];  
                for(int i=0;(i=in.read(buff))>0;)  
                {  
                    out.write(buff,0,i);  
                }  
                out.close();  
                in.close();  
            }  
              
        } finally  
        {  
            JdbcUtils.free(resultset, st, conn);  
        }  
    }  
}


1 回复 有任何疑惑可以回复我~
#1

Helios_ 提问者

非常感谢!
2016-05-01 回复 有任何疑惑可以回复我~
#2

Helios_ 提问者

我之前写了代码发现上传之后下载下来文件就损坏了。。能帮忙看看吗?
2016-05-01 回复 有任何疑惑可以回复我~

public void upload(String userid, InputStream is) throws SQLException{

Connection conn=Init.getConnection();

String sql="" +

" insert file(userid,file) " +

" values(?,?) " ;

PreparedStatement ptmt=conn.prepareStatement(sql);

ptmt.setString(1, userid);

ptmt.setBinaryStream(2, is);

ptmt.execute();

}

public InputStream download(String userid) throws SQLException{

Connection conn=Init.getConnection();

String sql="" +

" select * from file " +

" where userid like ? ";

PreparedStatement ptmt=conn.prepareStatement(sql);

ptmt.setString(1, "%"+userid+"%");

ResultSet rs=ptmt.executeQuery();

rs.next();

return rs.getBinaryStream("file");

}


0 回复 有任何疑惑可以回复我~

首先,文件是可以保存到数据库中的,但是不建议这么做(Why请百度)。实际的做法是把文件的路径保存到数据库中,然后读取文件的话先是从数据库中读到路径,再根据路径得到文件,希望能帮到

1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

怎样将图片上传到数据库?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信