Batch upload of photos Java

Source: Internet
Author: User

Package com;

Import java. Io. bufferedinputstream;
Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenamefilter;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstream;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. util. arraylist;
Import java. util. List;

Import oracle. SQL. Blob;

Public class uploadphoto implements filenamefilter {
Private string prefix = "", suffix = "";
Public uploadphoto (string filterstr ){
Filterstr = filterstr. tolowercase ();
Int I = filterstr. indexof ('*');
Int J = filterstr. indexof ('.');
If (I> 0 ){
Prefix = filterstr. substring (0, I );
}
If (j> 0 ){
Suffix = filterstr. substring (J + 1 );
}
}
Public Boolean accept (File Dir, string name ){
// Todo auto-generated method stub
Boolean Yes = true;
Try {
Name = Name. tolowercase ();
Yes = (name. startswith (prefix) & (name. endswith (suffix ));
} Catch (nullpointerexception e ){
}
Return yes;
}

/**
* Get all the photos of the files under the project.
*
* @ Param File
* @ Return
*/
Public static list getphotoname (File file ){
List photolist = new arraylist ();
Filenamefilter filter = new uploadphoto ("*. jpg ");
File curdir = new file (file. getabsolutepath (),"");
String [] STR = curdir. List (filter );

For (INT I = 0; I <Str. length; I ++ ){
String first = STR [I]. substring (0, STR [I]. Length ()-4 );
For (INT m = 0; m <Str. length; m ++ ){
String [] photoname = {"", ""};
String str1 = STR [M]. substring (0, STR [M]. Length ()-4 );
If (str1.indexof ("_")>-1 ){
String [] names = str1.split ("_");
If (first. Equals (Names [0]) {
Photoname [0] = first;
Photoname [1] = str1;
Photolist. Add (photoname );
}
}
}
}
For (INT I = 0; I <Str. length; I ++ ){
Boolean ornot = false;
String first = STR [I]. substring (0, STR [I]. Length ()-4 );
For (INT m = 0; m <Str. length; m ++ ){
String str1 = STR [M]. substring (0, STR [M]. Length ()-4 );
If (str1.indexof ("_")>-1 ){
String [] names = str1.split ("_");
If (first. Equals (Names [0]) {
Ornot = true;
Break;
}
}
}
If (ornot = false ){
If (first. indexof ("_") =-1 ){
String [] photoname = {first, "large "};
Photolist. Add (photoname );
}
}
}
For (INT I = 0; I <Str. length; I ++ ){
Boolean ornot = false;
String first = STR [I]. substring (0, STR [I]. Length ()-4 );
If (first. indexof ("_")>-1 ){
String [] Name = first. Split ("_");
For (INT m = 0; m <Str. length; m ++ ){
String str1 = STR [M]. substring (0, STR [M]. Length ()-4 );
If (str1.indexof ("_") =-1 ){
If (name [0]. Equals (str1 )){
Ornot = true;
Break;
}
}
}
If (ornot = false ){
String [] photoname = {"small", first };
Photolist. Add (photoname );
}
}
}
Return photolist;
}
/**
* Batch upload of processed services
*
* @ Param name
* @ Param filedir
*/
Public static void initialize photo (list namelist, file filedir ){
Preparedstatement pstmt = NULL;
Resultset rs = NULL;
String workidno = NULL;
String workname = NULL;
Blob blob = NULL;
Connection conn = NULL;
String name = NULL;
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
Conn = drivermanager. getconnection (
"JDBC: oracle: thin :@ 10.140.137.6: 1521: fzjclk", "fzjc", "fzjc ");
Conn. setautocommit (false );
For (INT I = 0; I <= namelist. Size (); I ++ ){
String [] photoname = (string []) namelist. Get (I );
Pstmt = conn. preparestatement ("select work_idno, work_name from fzjc_hum_info where work_name =? ");
If ("small". Equals (photoname [0]) {
Name = photoname [1];
The system. Out. println (name + "half-length photo does not exist! ");
} Else if ("large". Equals (photoname [1]) {
Name = photoname [0];
The system. Out. println (name + "photo does not exist! ");
} Else {
Name = photoname [0];
}
Pstmt. setstring (1, name );
Rs = pstmt.exe cutequery ();
If (Rs. Next ()){
Workidno = Rs. getstring ("work_idno ");
Workname = Rs. getstring ("work_name ");
} Else {
System. Out. println ("the database does not contain a record named [" + name +. ");
Continue;
}
Rs. Close ();
Pstmt. Close ();
Pstmt = conn. preparestatement ("delete from fzjc_hum_photo where work_idno =? ");
Pstmt. setstring (1, workidno );
Pstmt.exe cute ();
Pstmt = conn. preparestatement ("insert into fzjc_hum_photo (work_idno, work_photo, work_allphoto) values (?, Empty_blob (), empty_blob ())");
Pstmt. setstring (1, workidno );
Pstmt.exe cuteupdate ();
Pstmt. Close ();
If (photoname [0]! = NULL ){
File filephto = new file (filedir + "//" + photoname [0] + ". jpg ");
Pstmt = conn. preparestatement ("select work_photo from fzjc_hum_photo where work_idno =? For Update ");
Pstmt. setstring (1, workidno );
Rs = pstmt.exe cutequery ();
If (Rs. Next ())
Blob = (BLOB) Rs. getblob ("work_photo ");
Rs. Close ();
Fileinputstream fin = new fileinputstream (filephto );
Inputstream streamin = new bufferedinputstream (FIN );
Outputstream streamout = blob. getbinaryoutputstream ();
Int bytesread = 0;
Byte [] buffer = new byte [8192];
While (bytesread = streamin. Read (buffer, 0, 8192 ))! =-1 ){
Streamout. Write (buffer, 0, bytesread );
}
Streamout. Close ();
Streamin. Close ();
/** Close */
Pstmt = conn. preparestatement ("Update fzjc_hum_photo set work_photo =? Where work_idno =? ");
Pstmt. setblob (1, blob );
Pstmt. setstring (2, workidno );
Pstmt.exe cuteupdate ();
Pstmt. Close ();
Conn. Commit ();
The system. Out. println (workname + "half-length photo is uploaded successfully! ");
}
If (photoname [1]! = NULL ){
File filephto = new file (filedir + "//" + photoname [1]
+ ". Jpg ");
Pstmt = Conn
. Preparestatement ("select work_allphoto from fzjc_hum_photo where work_idno =? For Update ");
Pstmt. setstring (1, workidno );
Rs = pstmt.exe cutequery ();
If (Rs. Next ())
Blob = (BLOB) Rs. getblob ("work_allphoto ");
Rs. Close ();

Fileinputstream fin = new fileinputstream (filephto );
Inputstream streaminall = new bufferedinputstream (FIN );
Outputstream streamoutall = blob. getbinaryoutputstream ();
Int bytesreadall = 0;
Byte [] bufferall = new byte [1, 8192];
While (bytesreadall = streaminall. Read (bufferall, 0, 8192 ))! =-1 ){
Streamoutall. Write (bufferall, 0, bytesreadall );
}
Streamoutall. Close ();
Streaminall. Close ();
/** Close */
Pstmt = conn. preparestatement ("Update fzjc_hum_photo set work_allphoto =? Where work_idno =? ");
Pstmt. setblob (1, blob );
Pstmt. setstring (2, workidno );
Pstmt.exe cuteupdate ();
Pstmt. Close ();
Conn. Commit ();
System. Out. println (workname + "full body photo uploaded successfully! ");
}
}
} Catch (exception e ){
System. Out. println ("uploaded! ");
} Finally {
Try {
If (RS! = NULL)
Rs. Close ();
If (pstmt! = NULL)
Pstmt. Close ();
If (Conn! = NULL)
Conn. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}
/** Program entry */
Public static void main (string [] ARGs) throws ioexception {
System. Out. Print ("Enter the directory of the photo :");
Bufferedreader in = new bufferedreader (New inputstreamreader (system. In ));
String filename = in. Readline ();
File filedir = new file (filename );
List namelist = getphotoname (filedir );
Intophoto (namelist, filedir );
System. Out. println ("you can close the window ");
}
}

Related Article

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.