public String addArticle(){
        System.out.println("-----=---------------==========");
System.out.println(fileContentType + "****" + fileFileName);        
        Article article=new Article();
        article.setWDate(new Date());
//        article.setArtText(Hibernate.createBlob(getStringParameter("aText").getBytes()));
        try {
            FileInputStream input=new FileInputStream(file);
//            String path=ServletActionContext.getRequest().getRealPath("/") + "temp";
//            File f=new File(path);
//            if(!f.exists()){
//                System.out.println("file");
//                f.mkdirs();
//            }
//            FileOutputStream out=new FileOutputStream(new File(path +"\\" + fileFileName));
//            byte[] b=new byte[1024]; 
//            int len=0;
//            while((len=input.read(b))>0){
//                out.write(b, 0, len);
//            }
            Blob b=Hibernate.createBlob(input);
            article.setPhoto(b);//            out.close();            input.close();
        getEntityDao().save(article);        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "showArt";
    }
后台报这异常Hibernate: insert into Article (Photo, WDate) values (?, ?)
2010-04-30 11:04:35,468 WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: 1064, SQLState: 42000>
2010-04-30 11:04:35,468 ERROR [org.hibernate.util.JDBCExceptionReporter] - <You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't”??>????L?    ?·`??????-<:g¨?°?????????lw|ciJ??\01H?au?\0$?J?*?&\05?eO?8—u<' at line 1>卡住了 ... 不知道为什么乱码
和包有关系吗?

解决方案 »

  1.   

    你换用HDSQL试试
    可能有的MYSQL版本并不是很完善在一个就是看看你自己数据库中的各种约束
      

  2.   

    现在发现txt和其他文件可以上传成功  好像就是jpg的不能上传
      

  3.   

    blob什么类型的都能存,jpg不能存,你先不要用hib试试能不能存。
    这个问题你不是在另一个梯子里面说是没有关流还是先关了流引起的吗?
    再次提示注意blob4种类型的大小
    纯jdbc操作的例子:
    package test.mysql;import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;public class TestBlob { public static void insBlob(String fileName) {
    Connection con = null;
    PreparedStatement ps = null;
    BufferedInputStream bis = null;
    try {
    con = MConnection.getConn();
    String sql = "insert into testblob(image) values(?)";
    ps = con.prepareStatement(sql);
    File file = new File(fileName);
    bis = new BufferedInputStream(new FileInputStream(file));
    ps.setBinaryStream(1, bis, new Long(file.length()).intValue());
    int ret = ps.executeUpdate();
    System.out.println("insert file finished:" + ret);
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (bis != null) {
    try {
    bis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (ps != null) {
    try {
    ps.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (con != null) {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    } public static void updBlob(int id, String fileName) {
    Connection con = null;
    PreparedStatement ps = null;
    BufferedInputStream bis = null;
    try {
    con = MConnection.getConn();
    String sql = "update testblob set image=? where id=?";
    ps = con.prepareStatement(sql);
    File file = new File(fileName);
    bis = new BufferedInputStream(new FileInputStream(file));
    ps.setBinaryStream(1, bis, new Long(file.length()).intValue());
    ps.setInt(2, id);
    int ret = ps.executeUpdate();
    System.out.println("update file finished:" + ret);
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (bis != null) {
    try {
    bis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (ps != null) {
    try {
    ps.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (con != null) {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    } public static void selBlob(int id, String saveFileName) {
    InputStream is = null;
    OutputStream os = null;
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
    con = MConnection.getConn();
    String sql = "select image from testblob where id = ?";
    ps = con.prepareStatement(sql);
    ps.setInt(1, id);
    rs = ps.executeQuery();
    if (rs.next()) {
    is = rs.getBinaryStream(1);
    os = new FileOutputStream(saveFileName);
    byte[] bytes = new byte[1024];
    int read = 0;
    while ((read = is.read(bytes)) != -1) {
    os.write(bytes, 0, read);
    }
    }
    System.out.println("get file finished!");
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (is != null) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (os != null) {
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (ps != null) {
    try {
    ps.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (con != null) {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    } public static void main(String[] args) {
    String fileName = "f:/notebook00.gif";
    TestBlob.insBlob(fileName);
    String saveFileName = "f:/notebook01.bak.gif";
    TestBlob.selBlob(1, saveFileName);
    TestBlob.updBlob(1, fileName); }}
      

  4.   

    你好 jdbc直接插入产生的问题是一样Exception in thread "main" com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$?ò???\00GpysA?—y?8B?)T*??—????[[?CKqkBbO!???ht§ rj??#N\0@??s?oaeO2??:V63??' at line 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
    at com.jdx.webdemoart.action.ArticleAction.main(ArticleAction.java:112)
      

  5.   

    我发现插入成功后到数据库BLOB别的类型的数据在里面存的都是16进制
    是不是图片是不是二进制文件 强转16进制报异常
      

  6.   

    不想了 会不会是mysql的版本有问题  
    现在往sqlserver里插都没问题
      

  7.   

    上面代码也没问题啊 
    public static void main(String[] args) throws Exception{
    String url="jdbc:mysql://127.0.0.1:3306/test";

    String user="test";
    String password="sa1234";
     
    Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url,user,password);
        String sql = "insert into article(Photo) values(?)";
        PreparedStatement  ps = (PreparedStatement) con.prepareStatement(sql);
            File file = new File("d:/1.jpg");
            BufferedInputStream  bis = new BufferedInputStream(new FileInputStream(file));
            ps.setBinaryStream(1, bis, new Long(file.length()).intValue());
            int ret = ps.executeUpdate();
           ps.close();
           bis.close();
           con.close();
    }
    这是jdbc得  把图片文件改成别的文件都行 就是图片文件不行  
      

  8.   

    看看你的跟我的有啥区别?注意红色部分
    mysql> show create table testblob;
    +----------+---------------------------------
    | Table    | Create Table
    +----------+---------------------------------
    | testblob | CREATE TABLE `testblob` (
      `id` int(11) NOT NULL auto_increment,
      `image` mediumblob,
      PRIMARY KEY  (`id`)
    ENGINE=InnoDB DEFAULT CHARSET=utf8  |
    +----------+---------------------------------
    1 row in set (0.06 sec)
      

  9.   

    CREATE TABLE `article` (
      `Id` int(11) NOT NULL auto_increment,
      `Title` varchar(255) default NULL,
      `Photo` longblob,
      `ArtText` longblob,
      `WDate` datetime default NULL,
      PRIMARY KEY  (`Id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=gbk那现在怎么修改 我页面都是utf-8
      

  10.   


    看样子可能快要乱码了,数据库的编码gbk,页面utf8,编码不统一很容易乱掉。
    怎么改,去看下mysql的官方文档,我也不知道怎么改已存在的表的字符集