通过Spring MVC将二进制文件上传到MySQL的正确方法 [英] Correct way to upload binary file to MySQL through Spring MVC

查看:172
本文介绍了通过Spring MVC将二进制文件上传到MySQL的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图上传文件到MySQL,但是我无法做到这一点。我正在使用云平台来运行Java Spring应用程序作为网站。

控制器:

  byte [] bytes = file.getBytes ); //文件是MultipartFile 
DFile newFile = new DFile(); //我的对象
newFile.setName(name); //名称列
newFile.setType(type); //
Blob blob = new SerialBlob(bytes); //从二进制
设置blob newFile.setData(blob);
fileService.insertFile(newFile); //调用插入到MySQL

Dao:两种不同的方式

  length =(int)blob.length(); 
byte [] b = blob.getBytes(1,length);
InputStream is = new ByteArrayInputStream(b);
LobHandler lobHandler = new DefaultLobHandler();
//方法1
jdbcTemplate.update(
sql,
new Object [] {dFile.getName(),dFile.getType(),
blob,} ,
new int [] {Types.VARCHAR,Types.VARCHAR,Types.BLOB});
//方法2
连接con;
PreparedStatement ps;
ps = con.prepareStatement(sql);
ps.setString(1,dFile.getName());
ps.setString(2,dFile.getType());
ps.setBinaryStream(3,is,length);

int count = ps.executeUpdate();

来自档案的原始资料

  0000-0010:ff d8 ff e0-00 10 4a 46-49 46 00 01-01 01 00 90 ...... JF IF ...... 
0000- 0020:00 90 00 00-ff db 00 43-00 02 01 01-02 01 01 02 ..... C ........
0000-0030:02 02 02 02- 02 02 02 03-05 03 03 03-03 03 06 04 ........ ........
0000-0040:04 03 05 07-06 07 07 07-06 07 07 08-09 0b 09 08 ........ ........



<从MySQL提供的数据

  0000-0010:ef bf bf ef -bf bd ef bf ef bf bd-00 10 4a 46 ........ ...... JF 
0000-0020:49 46 00 01-01 01 00 ef -bf bd 00 ef -bf bd 00 00 IF .... .. ........
0000-0030:ef bf bd ef-bf bd 00 43-00 02 01 01-02 01 01 02 ..... C ..... ...
0000-0040:02 02 02 02-02 02 02 03-05 03 03 03-03 03 06 04 ........ ........

我如何创建表格File:

  String create =CREATE TABLE File(
name varchar(255)character set utf8不为null,
类型varchar(64)字符集utf8不为null,
数据中断,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
主键(名称)
)ENGINE = MyISAM DEFAULT CHARSET = utf8;



事实:




  1. 该网站有一个新的phpAdmin页面来执行sql操作。旧的网络界面无法上传正确的图像文件。如果使用我的代码上传,错误的文件似乎是相同的。新的PhpAdmin有一个选项二进制时,上传和它的工作。
  2. 我使用UTF-8在所有的网络服务,包括CharsetFilter在web.xml和SQL字符集。 >
  3. 我使用类似的代码,但在控制器中反转来提供文件,如果我加载使用新的web phpAdmin加载文件,可以显示正确的图像文件



问题:



出了什么问题?我猜测它可能是字符编码。
如何解决这个问题?

解决方案

尝试使用下面的样式

  insert into File(name,type,data)values(good.bin,binary,0x01020304)


I am trying to upload file to MySQL, however I wasn't able to do that correctly. I am using a cloud platform to run Java Spring application as a web site.

Controller:

byte[] bytes = file.getBytes();    // file is MultipartFile
DFile newFile = new DFile();       // my object
newFile.setName(name);             // name column
newFile.setType(type);             // 
Blob blob = new SerialBlob(bytes); // set the blob from binary
newFile.setData(blob);
fileService.insertFile(newFile);   // calls to insert into MySQL

Dao: two different ways

        length = (int)blob.length();
        byte[] b = blob.getBytes(1, length);
        InputStream is=new ByteArrayInputStream(b);
        LobHandler lobHandler = new DefaultLobHandler();
        // method 1
        jdbcTemplate.update(
                sql,
                new Object[] { dFile.getName(), dFile.getType(),
                        blob,},
                new int[] {Types.VARCHAR, Types.VARCHAR, Types.BLOB});
        // method 2
        Connection con;
        PreparedStatement ps;
        ps = con.prepareStatement(sql);
        ps.setString(1, dFile.getName());
        ps.setString(2, dFile.getType());
        ps.setBinaryStream(3,  is, length);

        int count = ps.executeUpdate();

Original Data from file

0000-0010:  ff d8 ff e0-00 10 4a 46-49 46 00 01-01 01 00 90  ......JF IF......
0000-0020:  00 90 00 00-ff db 00 43-00 02 01 01-02 01 01 02  .......C ........
0000-0030:  02 02 02 02-02 02 02 03-05 03 03 03-03 03 06 04  ........ ........
0000-0040:  04 03 05 07-06 07 07 07-06 07 07 08-09 0b 09 08  ........ ........

Data served from MySQL

0000-0010:  ef bf bd ef-bf bd ef bf-bd ef bf bd-00 10 4a 46  ........ ......JF
0000-0020:  49 46 00 01-01 01 00 ef-bf bd 00 ef-bf bd 00 00  IF...... ........
0000-0030:  ef bf bd ef-bf bd 00 43-00 02 01 01-02 01 01 02  .......C ........
0000-0040:  02 02 02 02-02 02 02 03-05 03 03 03-03 03 06 04  ........ ........

How I created the table File:

  String create ="CREATE TABLE File (
  "name varchar(255) character set utf8 not null, 
  type  varchar(64) character set utf8 not null,    
  data mediumblob,
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  primary key (name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Fact:

  1. The web site has an old and a new phpAdmin page to perform sql actions. The old web interface wasn't able to upload correct image files. The wrong file seems to be the same if uploaded using my code. The new PhpAdmin has an option Binary when upload and it works.
  2. I am using UTF-8 in all web serving, including CharsetFilter in web.xml, and SQL character set.
  3. I am using similar code but reversed in Controller to serve file which can show correct image file if I load using the new web phpAdmin.

Question:

What went wrong? I am guessing it's possibly to be character encoding. How to fix the problem?

解决方案

Try to use following style

insert into File (name, type, data) values ("good.bin", "binary", 0x01020304)

这篇关于通过Spring MVC将二进制文件上传到MySQL的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆