Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Upload files to database and server

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

We need to do three steps to upload files.

① can choose to upload files on the page.

② can convert binary data into an byte array and store it in the database. Pay attention to the types of database fields.

③ saves the file to the server.

@ RequestMapping (value = "/ upload") public void upload (UpLoadFile uploadFile) throws Exception {String msg = "upload successful!" ; String originalFilename = uploadFile.getFile (). GetOriginalFilename (); long size = uploadFile.getFile (). GetSize (); String contentType = uploadFile.getFile (). GetContentType (); String uploadRoot = "xxx"; / / server root path String folder = ""; folder = uploadRoot + folder Folder = StringUtils.replace (folder, "\", "/"); File dir = new File (folder); if (! dir.exists ()) {if (! dir.mkdirs ()) {throw new Exception ("creating directory failed!") }} folder + = "/" uploaded file name "; File outFile = new File (folder); byte [] appwar = uploadFile.getFile () .getBytes (); / / File to byte byte type / / me.setAppwar (appwar) / / the longblob type is used in the MySQL database, and the maximum single storage file is 4G / / this.mobileverDao.insert (me); try {uploadFile.getFile (). TransferTo (outFile); / / the address where the file is uploaded to the server / / use the transferTo (dest) method to write the uploaded file to the file specified on the server. } catch (IllegalStateException e) {throw new Exception ("upload file is error!\ n" + e.getMessage ());} / / upload successfully}

The file has been stored in bytes in the database.

There are four types of BLOB in MySQL database

TINYBLOB variable length binary data, up to 255byte

BLOB variable length binary data, up to 2 to the power of 16-1 byte or 64K

MEDIUMBLOB variable length binary data, up to 2 to the 24th power-1 byte or 16m

LONGBLOB variable-length binary data, up to 2 to the power of 32-1 byte or 4G

The only difference between these types is in the maximum size of the stored file.

In Oralce database, big data is divided into two types: BLOB and BFILE, and BLOB is divided into the following three types.

BLOB is called binary large object (Binary Large Object). It is used to store large binary objects in the database. The maximum size that can be stored is 4G bytes CLOB CLOB, which is called character large object (Character Large Object). It is similar to the LONG data type, except that CLOB is used to store large single-byte character blocks in the database and does not support character sets of varying widths. The maximum size that can be stored is 4G bytes NCLOB the NCLOB data type based on the national language character set is used to store large blocks of fixed-width single-byte or multi-byte characters in the database, and character sets of different widths are not supported. The maximum storage size is 4G bytes

BFILE when the size of a large binary object is 4G bytes, the BFILE data type is used to store it in an operating system file outside the database; when its size is less than 4G bytes, it is stored in an operating system file inside the database, and the BFILE column stores the file locator, which points to a large binary file on the server.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report