Package com.zy.oa.util;
Import java.io. *;
Import java.util. *;
Import java.sql. *;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.servletinputStream;
Import javax.servlet.servletException;
/ **
* This class provides file upload service
* /
Public class upload {
Private static string newline = "/ n";
Private string UploadDirectory = "D: /jakarta-tomcat-4.0.1/webapps/oa/upload";
Private string contenttype = ""
Private string characterencoding = "";
Private string strfilename = "";
PRIVATE long ifilesize = 0;
Private hashtable value = null;
Private vector filenames = NULL;
Private vector OriginalFileNames = NULL;
Private vector existFilenames = null;
PRIVATE STRING FIETEXT = NULL;
Private string OriginalFileText = NULL;
Public Upload () {
VALUES = new hashtable ();
Filenames = new vector ();
OriginalFileNames = new vector ();
AttachmentManage ManageFilenames = new attachmentManage ();
Global global = new global ();
Try {
UploadDirectory = Global.getvarvalue (global.uploaddir);
} catch (exception e) {
E.PrintStackTrace ();
}
}
Private string getFilename (string s) {
INT i = s.lastIndexof ("//");
IF (i <0 || i> = s.length () - 1) {
I = s.lastIndexof ("/");
IF (i <0 || i> = s.length () - 1)
Return S;
}
Strfilename = s.substring (i 1);
// logger.log (strfilename);
Return S.SUBSTRING (i 1);
}
Public string getuploadFileName () {
Return strfilename;
}
Public long getFilesize () {
Return ifilesize;
}
Public void setuploaddirectory (string s) {
UploadDirectory = S;
Logger.log ("Upload path is:" s);
}
Public void setContentType (string s) {contentType = S;
Int J;
IF ((j = contenttype.indexof ("boundary ="))! = -1) {
ContentType = ContentType.Substring (J 9);
CONTENTTYPE = "-" ContentType;
}
}
Public void setcharacterencoding (string s) {
Characterencoding = s;
}
Private string getname (string src) {
INT i = src.indexof ("name = /");
INT j = src.lastIndexof ("/");
Return New String (src.substring (i 6, j));
}
Private string getValue (String src) {
Return NULL;
}
Public HashTable getValues () {
Return this.values;
}
Public Vector getFilenames () {
Return this.filenames;
}
Public void UploadFile (httpservletRequest Req) Throws ServletException,
IOEXCEPTION {
//Logger.log (req.getcharacterencoding ());
//Logger.log (req.getContentType ());
SetcharacterenceRencoding ());
SetContentType (Req.getContentType ());
UploadFile (Req.getInputStream ());
}
Private Void UploadFile (ServletInputStream ServletInputStream) THROWS
ServletException, IOException {
AttachmentManage ManageFilenames = new attachmentManage ();
EXISTFILENAMES = ManageFilenames.getNameList (this.uploaddirectory);
String filename = NULL;
BYTE linebyte [] = new byte [4096];
INT ai [] = new int [1];
String line = NULL;
String filetExtTemp = ""
// while (LineByte, Ai, ServletInputStream, Characterencoding)! = null)
// {Logger.log (line);
Do {
// Get the name of the file
// If the end of the last party includes the file name, the line is processed directly.
IF (line! = null && line.indexof ("filename =")> = 0) {
// String aline = "";
// aline = new string (line);
INT j = line.indexof ("name =");
IF (j> = 0) {
Line = line.substring (j 6);
FileTextTemp = line.substring (0, line.indexof ("/"));
}
INT i = line.indexof ("filename =");
IF (i> = 0) {
Line = line.substring (i 10);
IF ((i = line.indexof ("/"))> 0)
Line = line.substring (0, i);
Break;
}
// int j = aline.indexof ("name =");
}
Else {
// The last line read out does not include the file name, continue to read
While (LineByte, Ai, ServletInputStream,
Characterencoding)))! = Null) {
// Logger.log (line);
// Get the NAME domain of the file
INT j = line.indexof ("name =");
IF (j> = 0) {
Line = line.substring (j 6);
FileTextTemp = line.substring (0, line.indexof ("/"));
}
INT i = line.indexof ("filename =");
IF (i> = 0) {
Line = line.substring (i 10);
IF ((i = line.indexof ("/"))> 0)
Line = line.substring (0, i);
Break;
}
}
}
// filename is the path and file name of the file
FILENAME = line;
// Get the file name If the file name is normal, start reading the upload file.
IF (filename! = null && (filename.indexof ("/") == -1)) {
INT conflict_id = 0;
FileName = getFileName (filename);
/ / Preserve the original information
IF (! filetextTemp.equals ("text"))
THIS.ORIGINALFILENAMES.ADD (New String (filename));
Else
THIS.ORIGINALFIETEXT = filename;
// If the attachment list is changed to prevent conflicts
IF (existFilenames.contains (filename) {
Try {
Conflict_id = KeyGenerator.getNewID ("conflict_id");
} catch (sqlexception e) {E.PrintStackTrace ();
Filename = conflict_id "_" filename;
}
// If you do not include this file in the file list and not a body, join
IF (! this.filenames.contains (filename) && (! filetextTemp.equals ("text")))))))))
This.FileNames.Add (New String (filename));
Else // Text Name FileText = New String (filename);
String ScontentType = Readline (Linebyte, Ai, ServletInputStream,
Characterencoding;
IF (scontentType! = null)
IF (ScontentType.Indexof ("Content-Type"> = 0)
Readline (Linebyte, Ai, ServletInputStream, Characterencoding;
File File = NULL;
FileOutputStream FileOutputStream = NULL;
Try {
FILE = New File (UploadDirectory, FileName);
FileOutputStream = New FileoutputStream (file);
} catch (exception e) {E.PrintStackTrace ();
String Tempstr = Readline (Linebyte, Ai, ServletInputStream,
Characterencoding;
// logger.log (Tempstr);
// read upload file
While (tempstr! = null) && (Tempstr.Indexof (this.contentType) == -1)) {
// logger.log (Tempstr);
FileOutputStream.write (Linebyte, 0, Ai [0]);
Tempstr = Readline (Linebyte, Ai, ServletInputStream,
Characterencoding;
Line = TempStr;
} // End while
// FileOutputStream.flush ();
FileOutputStream.Close ();
} // end if
// system.out.println ("line:" line);
} while (line! = null);
}
Private string readline (byte linebyte [], intloy [], servletinputstream
ServletinputStream, String Characterencoding) {
Try {
Ai [0] = servletinputStream.readLine (linebyte, 0, linebyte.length);
IF (Ai [0] == -1)
Return NULL;
} catch (ioexception _ex) {
_ex.printstacktrace ();
Return NULL;
}
Try {
IF (Characterencoding == NULL) {
// convert the given BYTE array into a string with a default encoding method.
Return New String (Linebyte, 0, Ai [0]);
} else {
// convert the given BYTE array into a string with a given encoding method.
Return New String (Linebyte, 0, Ai [0], Characterencoding;
}
} catCH (Exception_ex) {
_ex.printstacktrace ();
Return NULL;
}
}
Public string getuploaddirectory () {
Return UploadDirectory;
}
Public string getfiletext () {return filetext;
}
Public Vector getoriginalFileNames () {
Return OriginalFileNames;
}
Public string getoriginalfiletext () {
Return ORIGINALFIETEXT;
}
}