我想把一个图片以二进制形式上传到数据库中。请问各位应该如何把图片转换成二进制流和如何把二进制流转换成图片。谢谢!

解决方案 »

  1.   

    一般不是这么做,一般数据库存的是你图片的地址路径,一个url地址或者服务器的地址路径。而要是想上穿到服务器,首先是用相关上传插件把图片传到服务器某个路径上,最好自己编写套文件命名规范,日期时间什么的,然后把文件名放入相应的数据库中。需要时候就读取地址,加载文件。 
      

  2.   

    如果是直接保存到数据库的话,用blob类型存储,但一般都是保存地址就可以的
      

  3.   

    一般是上传到服务器的某个文件中,拼一个唯一的名字,把名字放进数据库,要用的时候,再去读方法是:如果是用struts的话,里面有个类是FormFile,直接就可以上传文件了
      

  4.   

    我的博客里面有
      http://hi.baidu.com/xhz12345/blog/item/f4c6821fa439f7cca68669e4.html
      

  5.   


    package amanda;   
      
    import java.io.File;   
    import java.io.FileInputStream;   
    import java.io.FileNotFoundException;   
    import java.io.FileOutputStream;   
    import java.io.IOException;   
    import java.io.InputStream;   
    import java.io.PrintWriter;   
    import java.sql.Connection;   
    import java.sql.PreparedStatement;   
    import java.sql.ResultSet;   
    import java.sql.SQLException;   
      
    import javax.servlet.ServletException;   
    import javax.servlet.http.HttpServlet;   
    import javax.servlet.http.HttpServletRequest;   
    import javax.servlet.http.HttpServletResponse;   
      
    public class WriteImage extends HttpServlet {   
      
        public void doGet(HttpServletRequest request, HttpServletResponse response)   
                throws ServletException, IOException {   
            process(request, response);   
        }   
      
        public void doPost(HttpServletRequest request, HttpServletResponse response)   
                throws ServletException, IOException {   
            process(request, response);   
        }   
      
        public void process(HttpServletRequest request, HttpServletResponse response)   
                throws ServletException, IOException {   
               
            // String strFile = request.getParameter("file");   
            // saveToDB(strFile);   
      
            ResultSet rs = null;   
      
            try {   
                Connection conn = DBUtil.getConnection();   
                String sql = "select * from image where image_id='Author.gif'";   
                PreparedStatement stmt = conn.prepareStatement(sql);   
                rs = stmt.executeQuery();   
      
                if (rs.next()) {   
                    java.sql.Blob b = rs.getBlob("image_bin");   
      
                    int size = (int) b.length();   
                    System.out.print(size);   
                    InputStream in = b.getBinaryStream();   
                    byte[] by = new byte[size];   
                    response.setContentType("image/jpeg");   
                    javax.servlet.ServletOutputStream out = response.getOutputStream();   
                    int bytesRead = 0;   
    //              out.print("以下是从数据库里取到的图片:\n\n");   
                    while ((bytesRead = in.read(by)) != -1) {   
                        out.write(by, 0, bytesRead);   
                    }   
                    out.flush();   
      
    //              response.setContentType("text/html");   
    //              out.print("image out finish 22!\n\n");   
                       
                    in.close();   
                    out.close();   
                }   
            } catch (Exception e) {   
                e.printStackTrace();   
            }   
            // response.sendRedirect("out.jsp");   
        }   
      
        private void saveToDB(String strFile) throws IOException {   
      
            File file = new File(strFile);   
            FileInputStream fis = new FileInputStream(file);   
      
            Connection conn = DBUtil.getConnection();   
            String sql = "insert into image values (?,?)";   
            PreparedStatement stmt;   
            try {   
                stmt = conn.prepareStatement(sql);   
      
                stmt.setString(1, file.getName());   
                stmt.setBinaryStream(2, fis, fis.available());// 数据库字段设置为   
                // mediumblob   
                stmt.executeUpdate();   
                // stmt.execute();   
                stmt.close();   
                fis.close();   
            } catch (SQLException e) {   
                e.printStackTrace();   
            }   
      
        }   
      
        // private void readblod() {   
        // ResultSet rs = null;   
        //   
        // Connection conn = DBUtil.getConnection();   
        // String sql = "select * from test where id='1'";   
        // PreparedStatement stmt = conn.prepareStatement(sql);   
        // rs = stmt.executeQuery();   
        //   
        // if (rs.next()) {   
        // java.sql.Blob b = rs.getBlob("pic");   
        //   
        // int size = (int) b.length();   
        // out.print(size);   
        // InputStream in = b.getBinaryStream();   
        // byte[] by = new byte[size];   
        // response.setContentType("image/jpeg");   
        // javax.servlet.ServletOutputStream sos = response.getOutputStream();   
        // int bytesRead = 0;   
        // while ((bytesRead = in.read(by)) != -1) {   
        // sos.write(by, 0, bytesRead);   
        // }   
        // in.close();   
        // sos.flush();   
        // }   
        // }   
      
        public void blobRead(String outfile, int picID) throws Exception {   
            FileOutputStream fos = null;   
            InputStream is = null;   
            byte[] Buffer = new byte[4096];   
      
            Connection conn = null;   
            PreparedStatement stmt = null;   
            ResultSet rs = null;   
            try {   
                conn = DBUtil.getConnection();   
                stmt = conn.prepareStatement("select pic from image where id=?");   
                stmt.setInt(1, picID); // 传入要取的图片的ID   
                rs = stmt.executeQuery();   
                rs.next();   
      
                File file = new File(outfile);   
                if (!file.exists()) {   
                    file.createNewFile(); // 如果文件不存在,则创建   
                }   
                fos = new FileOutputStream(file);   
                is = rs.getBinaryStream("pic");   
                int size = 0;   
      
                while ((size = is.read(Buffer)) != -1) {   
                    fos.write(Buffer, 0, size);   
                }   
      
            } catch (Exception e) {   
                e.printStackTrace();   
            } finally {   
                if (null != fos)   
                    fos.close();   
                if (null != rs)   
                    rs.close();   
                if (null != stmt)   
                    stmt.close();   
                if (null != conn)   
                    conn.close();   
            }   
        }   
      
    }   我借鉴过,可行 !