For example, after having suffered a lot of losses from Nn, I am determined to write this article. I would like to sum up the experience of uploading slice. The type may be incorrect. I believe everyone understands it. I just want to write down my ideas.
Previously, I used struts1.x to upload images. It used formfile to encapsulate the file parameters passed by the page, and struts2.x used file-type files, you can also encapsulate the filename and filecontenttype file types (it seems that this attribute is not the same as this one), encapsulate the data in the action, and then process it at the DaO layer, there is a difference here:
(Assume that the database field name is photo, the attribute name of the pojo object is photo, and the attribute name of the Action encapsulation file is photofile)
1: Oracle Database:
Pojo object attributes are set to blob type, and then use
Fileinputstream = new fileinputstream (photofile );
Hibernate. createblob (fileinputstream );
This method returns a blob type of data. In the hibernate ing file, <property name = "photo" column = "photo"/>. If there is a problem, add the type attribute and call gethibernatetemplate (). save.
2. MySQL database:
The attribute of pojo needs to be modified. The Blob type is not used here. The byte [] type is used instead,
Byte [] filebytes = NULL;
Fileinputstream = new fileinputstream (photofile );
Filebytes = new byte [fileinputstream. avaliable ()];
Fileinputstream. Read (filebytes );
Fileinputstream. Close ();
Save the image in binary format, and modify the hibernate ing file
<Property name = "photo" column = "photo" type = "binary"/> and then call the gethibernatetemplate. Save () method. Except for some, you do not need to modify it like Oracle. (At least I did. It succeeded ).
Note: struts has a size limit on File Upload. The default value seems to be a little larger than 2 MB (you can change the struts configuration), and MySQL does the same. If it is a large file, it is not recommended to store the data directly in the database. On the other hand, you must first modify the MySQL configuration file my. INI, add a max_allowed_packet (it looks like this, Google it), set the maximum allowed value to upload.