首页 新闻 会员 周边

(Java+MySQL)MySQL大字段(数据类型mediumblob)存储图片、doc文件失败,请问是什么原因?

0
悬赏园豆:10 [已解决问题] 解决于 2013-03-27 18:42

我在复习Java的JDBC部分时,参照书上做大字段存取的练习,遇到了问题,特来求助,希望大哥们帮我看下:
我建了一个数据表,表结构如下:
mysql> desc file_table;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| file_id | int(11) | NO | PRI | NULL | auto_increment |
| file_name | varchar(64) | YES | | NULL | |
| file_data | mediumblob | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+

//配置文件:

dbType=mysql
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/20130316
user=root
pass=123456
//代码片段如下
private static final String PROP_FILE_PATH = "conf/mysql.properties";
private static Connection conn = null;
private static PreparedStatement insertPstmt = null;
/**
* 读取配置文件→获取数据库连接→生成预编译对象
*/

private void init() {
    prop = new Properties();
    try {
        prop = MyUtil.initParam(PROP_FILE_PATH);
    } catch (FileNotFoundException e) {
        System.out.println("找不到配置文件: " + e);
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("加载配置文件出错: " + e);
        e.printStackTrace();
    }
    String driver = prop.getProperty("driver");
    String url = prop.getProperty("url");
    String user = prop.getProperty("user");
    String pass = prop.getProperty("pass");
    // 自定义方法,获取数据库连接
    conn = MyUtil.getConnection(driver, url, user, pass);
    String insertSQL = "insert into file_table values(null, ?, ?)";
    // 该对象执行插入语句后能够返回自动生成的键
    try {
        insertPstmt = conn.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
    } catch (SQLException e) {
        System.out.println("添加文件失败,原因:预编译插入语句失败: " + e);
        e.printStackTrace();
    }
}

/**
* 上传文件到数据表
* @param filePath
*/
private void upload(String filePath) {
    InputStream inStream = null;
    int lastIndexOfNode = filePath.lastIndexOf(".");
    int lastIndexOfBacklash = filePath.lastIndexOf("\");
    String fileName = filePath.substring(lastIndexOfBacklash + 1, lastIndexOfNode);
    System.out.println("filePath=" + filePath);
    System.out.println("fileName=" + fileName);
    int affect = 0;
    try {
        insertPstmt.setString(1, fileName);
        inStream = new FileInputStream(filePath);
        File file= new File(filePath);
        // 设置二进制流参数
        insertPstmt.setBinaryStream(2, inStream, file.length());
        affect = insertPstmt.executeUpdate();
        System.out.println("affect=" + affect);
        if (affect == 1) {
            System.out.println("文件(" + filePath + ")上传成功");
        }
    } catch (FileNotFoundException e) {
        System.out.println("找不到文件: " + e);
        e.printStackTrace();
    } catch (SQLException e) {
        // System.out.println("上传文件失败,原因向数据表添加记录出错: " + e);
        e.printStackTrace();
    } finally {
        // 回收IO资源,自定义的方法
        MyUtil.closeInputStream(inStream);
    }
}
public static void main(String[] args) {
    try {
        UploadFileTest uploadTest = new UploadFileTest();
        uploadTest.init();
        String txtPath = "C:\Documents and Settings\Administrator\桌面\temp.txt";
        String docPath = "C:\Documents and Settings\Administrator\桌面\temp.doc";
        String jpgPath = "C:\Documents and Settings\Administrator\桌面\temp.jpg";
        uploadTest.upload(txtPath);
        uploadTest.upload(docPath);
        uploadTest.upload(jpgPath);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //自定义方法,回收数据库资源
        MyUtil.closePreparedStatement(insertPstmt);
        MyUtil.closeConnection(conn);
    }
}
上面就是代码片段,出现的问题是:上传纯文本文件(.txt)正常;上传.doc(里边没有贴图的也可以上传成功,但是再取出doc文件后,打开就是乱码了,而且文件的大小也变了)、*.jpg文件上传失败(
com.mysql.jdbc.exceptions.jdbc4.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 '?#?§?×?BRP?M?v??á??i??R??;?p?+?\0??§c?e¤d+70<g?{~???????8X¨ X?A=ù?9' at line 1),抛出异常的是这一句affect = insertPstmt.executeUpdate();。
别人也曾指点我“文件上传只保存地址即可,也可减轻数据库负担,增加访问速度”。但这明显不是我想要的答案,我就是想知道①为什么会引发SQL异常(异常提示说我SQL语句有语法问题),②该怎么解决修复这个异常。希望经验丰富的哥哥姐姐们抽空指点一下小弟(why & how)。

蜀山客的主页 蜀山客 | 初学一级 | 园豆:191
提问于:2013-03-26 13:22
< >
分享
最佳答案
0

我在QQ群里提问,已经有人帮我指出问题的所在了。修改方式:在配置文件的url项添加一串字符串“?useUnicode=true&characterEncoding=utf8”,之后我也百度了这个用法的作用。还是要感谢“雷中雨”对我提问的回复,您是一个热心的人。

蜀山客 | 初学一级 |园豆:191 | 2013-03-27 18:41
其他回答(1)
0

...大体看了下代码,发现一个问题,你把路径insert到数据表中之前或之后应该把文件的内容上传到某个地方,比如一个盘符里面吧.可以使用io流读取写入指定的目录,insert到数据表中的路径替换为上传后的路径,这样执行代码后应该实现将文件上传到指定盘符下,又把路径保存到数据表中了.

收获园豆:10
雨中雷 | 园豆:274 (菜鸟二级) | 2013-03-26 16:51

大哥,sorry!之前是我的描述不到位(后来我重新编辑过了),我想要的结果不是把文件路径保存到数据表中。而是我想把一个文件用二进制流的形式存入大字段中。如果单纯地将一个文件路径保存到数据表,我不需要用大字段啊,随便用varchar数据类型的列保存就可以了。

支持(0) 反对(0) 蜀山客 | 园豆:191 (初学一级) | 2013-03-26 16:59

我在QQ群里提问,已经有人帮我指出问题的所在了。修改方式:在配置文件的url项添加一串字符串“?useUnicode=true&characterEncoding=utf8”,之后我也百度了这个用法的作用。还是要感谢您上面的对我提问的回复。

支持(1) 反对(0) 蜀山客 | 园豆:191 (初学一级) | 2013-03-27 18:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册