BLOB storage and reading of MyBatis

Source: Internet
Author: User
A brief introduction of the background environment, web development can not avoid photo accessories and so on, the original is saved to the hard disk under the file, storage reading is more convenient. Now the company has a hardware load balance, 6 servers, when users log in randomly assigned to one of the machines, 6 machines installed the same server,session not shared. Visit the same oracle11.2g, so that the original attachment may not work, you must ensure that 6 machines share files, think about directly into the database, and then do a local cache and so on. OK, here's the background.

The first step, did not do large-format access, online search data, n more data, the overall useful is not much, but also to find the bullet, start looking at the MyBatis and database field tables, bytes[] corresponding to the BLOB field, so the content in the Pojo class set type is byte[ Resultmap is set in],mapper.xml, field content set <result property= "content" column= "content" jdbctype= "BLOB" typehandler= " Com.ibatis.sqlmap.engine.type.BlobTypeHandlerCallback "/>

The results are always reported "ora-01461" errors, online inquiries, the basic meaning is beyond the length of the. But clearly is a large segment, the picture size is 70k, can not exceed the length

1 The byte defined as a length byte[0] = 1, continue to insert the database, successful. But it doesn't fit my needs.

2 continue to search on the internet, said to use byte[] This form of words, 1000-2000 between the bytes will be reported ' ora-01461 ' ERROR, the internet has to write oraclelobhandler practice, but he used is hibernate, large large section is CLOB, in the lazy purpose of direct neglect

3 finally found a useful article, my overall mileage is also in accordance with his method to http://www.360doc.com/content/06/0913/13/6272_206215.shtml, summed up the inside mentioned

1 "Pojo class can not use byte[], also can not use a blob, both of which I have used is indeed invalid, using the object

2 "MAPPER.MXL in the Resultmap to remove the Typehandler, I also suspect that the type of processor is not very good, can not convert the blob into byte[, etc., and so on, it is recommended that there is time for students to write a try again, the feeling should be able to succeed, Or you can change to object. Coercion type conversion is not expected.

3 Oracle Save large data, first insert a empty_blob () placeholder, take up the Blob field, followed by the query out of the big segment of the stream to write.

4 in the stream reading, the discovery of Blob.getbinaryoutputstream (), has become obsolete, and then changed to the recommended Blob.setbinarystream (0), always prompt "Invalid parameters ...", The view was found to be an oracle-driven problem, when the environment was oracle11.2g+ojdbc6.jar+jdk1.6, View Setbinarystream This API is said to be suitable for jdbc3.0, should be satisfied, by the replacement found that Ojdbc6,ojdbc5 have problems, replaced by 1.4JDK Ojdbc14.jar will not be a problem, but the higher the drive, the higher the efficiency, the less the bug, so has been used The method of the time.

The following is the main code I store and read:

Pojo class:

[HTML] View plaincopy on code to see a snippet derived from my Code slice

public class Attachment {
Private String ID;
ID of the private String group;//category, including message attachments, photos, and so on
private String name;
Private String type;
private String size;
Private String author;
Private Object content;
Public Object getcontent () {
return content;
}
public void SetContent (Object content) {
this.content = content;
}
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String Getgroup () {
return group;
}
public void Setgroup (String group) {
This.group = Group;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetType () {
return type;
}
public void SetType (String type) {
This.type = type;
}
Public String GetSize () {
return size;
}
public void SetSize (String size) {
this.size = size;
}
Public String Getauthor () {
return author;
}
public void Setauthor (String author) {
This.author = author;
}

}

[HTML] View plaincopy on code to see a snippet derived from my Code slice

Resultmap and SQL in Map.xml:

[Java] View plaincopy on code to see a snippet derived from my Code slice

<resultmap id= "Attachmentresultmap" type= "Attachment" >
<result property= "id" column= "id"/>
<result property= "group" column= "GroupId"/>
<result property= "name" column= "FileName"/>
<result property= "type" column= "FileType"/>
<result property= "Size" column= "FileSize"/>
<result property= "Author" column= "Author"/>
<result property= "Content" column= "content" jdbctype= "BLOB"/>
</resultMap>
<insert id= "insertattachment" parametertype= "Attachment" >
Insert into Bop_attachment (id,filename,filetype,filesize,author,content,groupid)
VALUES (#{id},#{name},#{type},#{size},#{author},empty_blob (), #{group})
</insert>

The main methods of service layer storage:

[Java] View plaincopy on code to see a snippet derived from my Code slice

@Override
public void insertattachment (attachment attachment,file File)
Throws Attachmentserviceexception {
Attachmentdao.insertattachment (attachment);
Attachment att = Attachmentdao.queryattachmentbyid (Attachment.getid ());
Blob content = (BLOB) att.getcontent ();
FileInputStream FIS = null;
OutputStream ops = null;
try {
OPS = Content.getbinaryoutputstream ()//temporary use of this deprecated method
OPS = Content.setbinarystream (0);//ojdbc14 support, ojdbc6,5 does not support
FIS = new FileInputStream (file);
byte[] data = null;
data = Filecopyutils.copytobytearray (FIS);
Ops.write (data);
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (fis!=null) {
Fis.close ();
}
if (ops!=null) {
Ops.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
}
}



Servlet layer for picture display:

[Java] View plaincopy on code to see a snippet derived from my Code slice

Attachmentservice as = (Attachmentservice) springutils.getcontext (). Getbean ("Attachmentservice");
Attachment attachment = NULL;
try {
Attachment = As.queryattachmentbyid (Attachmentid);
catch (Attachmentserviceexception ae) {
Logger.error (AE);
throw new Servletexception (AE);
}
String Realpath = RootDir + attachment.getname ();
Response.setcontenttype (Attachment.gettype ());
Response.setcontentlength (Integer.parseint (Attachment.getsize ()));
Response.setheader ("Content-disposition", "attachment; Filename= "
+ Java.net.URLEncoder.encode (Attachment.getname (), "UTF-8")
+ ';');
InputStream in = null;
Blob content = (BLOB) attachment.getcontent ();
try {
In=content.getbinarystream ();
catch (SQLException e) {
E.printstacktrace ();
}
Bufferedoutputstream BOS = new Bufferedoutputstream (Response.getoutputstream ());
int n;
while ((N=in.read ())!=-1) {
Bos.write (n);
}
Bos.flush ();
Bos.close ();
In.close ();


Reproduced from: http://blog.csdn.net/bluesky5219/article/details/36427789

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.