The procedure is as follows:
Package linkman. helper;
Import java. io .*;
Import java. util .*;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. ServletInputStream;
Public class UpLoadBean {
Private String SavePath = null;
Private String FileName = null;
Private String Sperator = null;
Private String ErrMsg = "Normal Return ";
Private byte [] bt = new byte [0, 4096];
Private byte [] prebt = new byte [1, 4096];
Private int t =-1;
Private long FileSize =-1;
Private boolean UserDefineFileName = false;
//////////////////////////////////////// /////////
Public UpLoadBean (){
FileName = bringFileName ();
}
Public UpLoadBean (String savepath ){
SavePath = savepath;
FileName = bringFileName ();
}
//////////////////////////////////////// /////////
// File Name Information
Private String bringFileName (){
Calendar dt = Calendar. getInstance ();
String str = "" + dt. get (Calendar. YEAR) + dt. get (Calendar. MONTH) + dt. get (Calendar. DAY_OF_MONTH );
Str = str + dt. get (Calendar. HOUR) + dt. get (Calendar. MINUTE) + dt. get (Calendar. SECOND );
Return str;
}
Public void setFileName (String filename ){
FileName = filename;
UserDefineFileName = true;
}
Public String getFileName (){
Return FileName;
}
//////////////////////////////////////// /////////
// Upload the file main program
Public boolean UpLoad (HttpServletRequest request) throws Exception {
If (SavePath = null ){
SetErrorMessage ("the path for saving the uploaded file is not set ");
Return false;
}
Request. setCharacterEncoding ("UTF-8 ");
ServletInputStream in = request. getInputStream ();
//////////////////////////////////////// ////
// Obtain the delimiter of a row
T = in. readLine (bt, 0, bt. length );
If (t! =-1 ){
Sperator = new String (bt, 0, t );
Sperator = Sperator. substring (0, 28 );
T =-1;
}
//////////////////////////////////////// ////
// Get the extension and set the full name of the file
Do {
T = in. readLine (bt, 0, bt. length );
String s = new String (bt, 0, t );
Int index = s. indexOf ("filename = "");
If (index! =-1 ){
S = s. substring (index + 10 );
Index = s. indexOf (""");
S = s. substring (0, index );
Index = s. lastIndexOf (".");
If (! UserDefineFileName ){
FileName = FileName + s. substring (index); // you can specify the full name of a file.
}
T =-1;
}
} While (t! =-1 );
//////////////////////////////////////// ////
// Obtain the File Content
FileOutputStream out = new FileOutputStream (SavePath + "" + FileName );
T = in. readLine (bt, 0, bt. length );
String s = new String (bt, 0, t );
Int I = s. indexOf ("Content-Type :");
If (I =-1 ){
SetErrorMessage ("uploaded is not a file ");
Return false;
} Else {
In. readLine (bt, 0, bt. length); // remove an empty row.
T =-1;
}
Boolean state = true;
Long trancsize = 0;
Try {
T = in. readLine (bt, 0, bt. length );
While (t! =-1 ){
S = new String (bt, 0, t );
If (s. length ()> 28 ){
S = s. substring (0, 28 );
If (s. equals (Sperator )){
Break;
}
}
/*
If (bt [0] ==45 & bt [1] ==45 & bt [2] ==45 & bt [3] ==45 & bt [4] = 45) {
Break;
}
*/
If (FileSize! =-1 ){
If (trancsize> = FileSize ){
Throw new Exception ("uploading files too large ");
}
}
Out. write (bt, 0, t );
Trancsize + = t;
T = in. readLine (bt, 0, bt. length );
}
} Catch (Exception e ){
State = false;
Out. close ();
SetErrorMessage (e. getMessage ());
File tmp = new File (SavePath, FileName );
If (! Tmp. delete ()){
SetErrorMessage (e. getMessage () + ", clear unfinished ");
}
} Finally {
If (state ){
Out. close ();
}
Return state;
}
}
//////////////////////////////////////// /////////
// Upload File error message
Public void setErrorMessage (String errormessage ){
ErrMsg = errormessage;
}
Public String getErrorMessage (){
Return ErrMsg;
}
//////////////////////////////////////// /////////
// File size information
Public void setFileSize (long size ){
FileSize = size;
}
Public long getFileSize (){
Return FileSize;
}
} // End class