Reference: http://www.weste.net/2004/11-3/12250047377.HTML We often encounter problems when writing OA is the storage problem of employee picture files, and there are two ways to solve this problem.
1. Read the image on the web server in the JSP / HTML page, which is to put the picture (upload) to the web server, then read with the HTML statement:
2. That is to upload it into the database. About the Oracle database, it supports BLOB, and Clob, respectively corresponds to pictures and text (long strings)
Due to performance reasons, we still have to use the second method, and save it to the database is easier to manage, is it?
First, we have to solve the upload problem, here the FileUpload Class inside the Apache Commons assembly in the universal use.
Specific steps such as:
DiskfileUpload DFU = New DiskfileUpload ();
Dfu.setsizemax (100000000);
Dfu.setsizetHold (100000);
Dfu.setRepositoryPath ("f: // public");
Try {
List fileItems = dfu.parsequest (required);
Iterator i = fileItems.Item ();
While (I.hasNext ()) {
FileItem Fi = (fileItem) i.next ();
IF (! fi.isformfield ()) {
Name = FI.GETNAME ();
SIZE = fi.getsize ();
IF ((Name == Null || Name.equals (")) && size == 0)
CONTINUE;
}
Name = FI.GETNAME ();
SIZE = fi.getsize ();
INPUTSTREAM IS = fi.getinputStream ();
}
The above code is the code that the web server accepts uploaded. The reference file is already given in the upload text file I wrote. Today, I finally want to understand:
DFU.SetRepositoryPath ("f: // public");
It turned out that the escape character is also said that / n / t, etc., and it is necessary to print the backslash. In fact, this problem is originally known, but because the experience has not been written by the image, I feel very deep, it is very terrible. , Haha, my heart is a bit fear. It seems that the foundation is very important, then there is a little bit of small details, and then there is a Java IO problem. I suddenly found it when I read the IO when I read it Java. But I didn't pay attention!
The file has been uploaded by the above code. Then, we want to implement the JDBC data source link, the purpose is to insert the data into Oracle.
Context ctx = new initialContext ();
DataSource DS = (Datasource) CTX.lookup ("JDBC / AsDbCoreds");
CONN = ds.getConnection ();
Conn.setautocommit (false);
About Import Java.sql. * Javax.sql. * Java.naming. * No longer detailed
Then according to a very useful article, insert the blob type must first 1. Insert an empty
String insert = "Insert INTO UPLOADPICTURE"
"" ?, espty_blob () ";
2. Then find the cursor inside the blob of Oracle:
String Findcursor = "SELECT Content" "from UploadPicture"
"Where name =? for update";
Pay attention to this for update (note !!! Must add for update, this will lock the line until the line is modified, the guarantee does not generate concurrency. It is still difficult to understand, let go of it first)
3. Then modify
String Update = "Update UploadPicture"
"SET Content =?"
"Where name =?";
The question mark here is written for the prepaaredStatement parameter!
Write this program to use Oracle.Sql.blob Class, this class is used to operate the BLOB data type
When we get the ResultSet object
BLOB = (blob) rs.getblob (1);
I don't know how to deal with it. What is blob? String, int, long? I don't understand now! It is estimated that people on 9CBS don't understand, otherwise I will send a post for a long time, no one answer, maybe it is very bad, maybe It's too simple, everyone dismisses, it seems that I have to continue to catch up!
Not complaining, return to the program (always feel that his divergent thinking is very strong, it seems that the writing procedure can not be like this, thanks to java is a pure object-oriented language, if the process is troubles)
How do we handle this blob? Answer is, no matter what it is, write bufferedoutputstream out1 = new bufferedoutputstream (blob.getbinaryoutputstream ());
Here is the stream of buffering such as BLOB (Note getBinaryoutputStream () has not been used, must have a better way to replace!), Say the flow, I am still a little dizzy, there are a lot, I don't know if I use it. Which is better!
The foundation is very important, this is my oral zero, here is a stream reading and writing, some streams read bytes from files or other locations (eg, fileInputstream), written, write flow The section is combined with useful data (such as DataInputStream). When we read the numbers, you need to first recommend a fileInpustream, then, then pass the objects of the class to DataInputStream.
FileInputStream Fin = New FileInputStream ("Emp.dat");
DataInputStream DIN = New DataInputStream (FIN); // Pass FIN to DIN
Double s = din.readdouble ();
By default, the stream is not buffered, if you use the buffer is
DataInputStream DIN = New DataInputStream
New BufferedInputStream (New FileInputStream ("Emp.dat"))))))
With this understanding, it is also used.
BufferedoutputStream out1 = new bufferedoutputstream (blob.getbinaryoutputstream ());
It is to build a buffering object to blob. Note that OUT1 here is not OUT, otherwise the TEMP data cannot be printed when running!
Already ready to write, how can I read it?
BufferedInputStream in = New BufferedInputStream (IS);
InputStream is = fi.getinputStream ();
Read the picture as the input stream. Save as an IS object, then use it here, ready to read and write, we start working:
INT C;
While ((c = in.read ())! = - 1) {OUT1.WRITE (C);} in.close ();
Out1.close ();
By buffering one read data, then a write data.-1 is the end of the file,
Finally, when you read and write, we have to close the read and write object!
Program analysis is the case, and will also study this issue later, and finally pay attention,
<% @ Page ContentType = "Image / JPEG; Charset = GBK"%>
Not
<% @ page contenttype = "text / html; charset = GBK"%>
Otherwise, it is displayed in text - garbled.
Here, I have studied the procedures in Oralce in Oralce. About the display, I have to trouble, I have realized it with the information, and I will study it tomorrow.
// Plug into the upload picture to the database
<% @ page contenttype = "text / html; charset = GBK"%>
<% @ Page Import = "java.util. *"%>
<% @ Page Import = "java.io. *"%>
<% @ Page Import = "org.apache.commons. *"%>
<% @ page import = "org.apache.commons.fileUpload. *"%>
<% @ Page Import = "java.sql. *"%>
<% @ Page Import = "javax.sql. *"%>
<% @ Page Import = "javax.naming. *"%>
<% @ Page Import = "Oracle.sql. *"%>
hEAD>
<%
Request.SetCharacterencoding ("GBK");
String name = NULL;
Long size = 0;
Connection conn = NULL;
String insert = "Insert INTO UPLOADPICTURE"
"" ?, espty_blob () ";
String FindCursor = "SELECT Content"
"from UploadPicture"
"Where name =? for update";
String Update = "Update UploadPicture"
"SET Content =?"
"Where name =?";
BLOB BLOB = NULL;
InputStream IS = NULL;
DiskfileUpload DFU = New DiskfileUpload ();
Dfu.setsizemax (100000000);
Dfu.setsizetHold (100000);
Dfu.setRepositoryPath ("f: // public");
Try {
List fileItems = dfu.parsequest (required);
Iterator i = fileItems.Item ();
While (I.hasNext ()) {
FileItem Fi = (fileItem) i.next ();
IF (! fi.isformfield ()) {
Name = FI.GETNAME ();
SIZE = fi.getsize ();
IF ((Name == Null || Name.equals (")) && size == 0)
CONTINUE;
}
Name = FI.GETNAME ();
SIZE = fi.getsize ();
IS = fi.getinputstream ();
}
Context ctx = new initialContext ();
DataSource DS = (Datasource) CTX.lookup ("JDBC / AsDbCoreds");
CONN = ds.getConnection ();
Conn.setautocommit (false);
// Step 1
PreparedStatement PS = conn.preparestatement (Insert);
Ps.setstring (1, name);
INT a = ps.executeUpdate ();
IF (a> 0)
Out.println ("INSERT SUCCESS!" "
");
// Step 2
PS = conn.preparestatement (FindCursor);
Ps.setstring (1, name);
ResultSet RS = ps.executeQuery ();
While (rs.next ())
{
BLOB = (blob) rs.getblob (1);
Out.println ("Find Cursor Success!" "
");
Out.println ("Cursor:" BLOB "
");
// step 3
PS = conn.preparestatement (Update);
Ps.setblob (1, blob);
Ps.setstring (2, name);
ps.executeUpdate ();
ps.close ();
BufferedoutputStream out1 = new bufferedoutputstream (blob.getbinaryoutputstream ());
BufferedInputStream in = New BufferedInputStream (IS);
INT C;
While ((c = in.read ())! = - 1) {OUT1.WRITE (C);}
In.Close ();
Out1.close ();
Out.println ("Update Success!" "
");
CONN.COMMIT ();
}
Catch (SQLException SE)
{se.printstacktrace ();
FileUploadexception Fue)
{Fue.PrintStackTrace ();}
%>
body>
html>
/ / Display image in the database <% @ page contentty = "image / jpeg; charset = GBK"%>
<% @ Page Import = "java.sql. *"%>
<% @ Page Import = "javax.sql. *"%>
<% @ Page Import = "javax.naming. *"%>
<% @ Page Import = "java.io. *"%>
<% @ Page Import = "com.sun.image.codec.jpeg. *"%>
<% @ Page Import = "javax.imageio. *"%>
<% @ Page Import = "java.util. *"%>
<% @ Page Import = "java.awt.image. *"%>