Uploading files to databases and servers

Source: Internet
Author: User

Uploading files We need to do three steps.

① in the page can choose File upload;

② is able to convert binary data into a byte array and then into the database, paying attention to the type of database field;

③ Save the file to the server.

@RequestMapping (value =  "/upload") public void upload (uploadfile uploadfile)   throws exception {string msg =  "uploaded successfully! "; 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 +=  "/"  +  "file name uploaded"; File outfile = new file (folder); Byte[] appwar = uploadfile.getfile (). GetBytes ();//  file to byte  byte type// me.setappwar (Appwar); Lo in//mysql databaseNgblob type to store, maximum single storage file is 4g// this.mobileverdao.insert (me); Try {uploadfile.getfile (). TransferTo (OutFile) The;//  file is uploaded to the server's address//using 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 Success}


The file is stored in bytes as a database.

The type of blob in the MySQL database is divided into the following four
  • tinyblob variable Long binary data, up to 255 bytes

  • BLOB variable Long binary data, up to 2 of 16 squares-1 bytes or 64K

  • mediumblob variable Long binary data, up to 2 of 24 squares-1 bytes or 16M

  • longblob variable Long binary data, up to 2 of 32 squares-1 bytes or 4G

  • The only difference between these types is that the maximum size of the stored file is different.

The large data types in the Oralce database are divided into BLOBs and bfile, two of which are divided into the following three types of BLOBs
BLOBs are all called binary Large objects (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 are all called character large objects (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 chunks of fixed-width single-byte or multibyte characters in the database and does not support character sets of varying widths. The maximum size that can be stored is 4G bytes
BFILE when large binary objects are large in size and 4G bytes, the BFILE data type is used to store them in operating system files outside the database, and when they are less than 4G bytes, they are stored in the operating system files inside the database, and the BFILE column stores the file locator. This locator points to a large binary file on the server.




This article is from the "Jianbo" blog, make sure to keep this source http://jianboli.blog.51cto.com/12075002/1925642

Uploading files to databases and servers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.