Package com. upload;
Import java. io .*;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. ServletInputStream;
Import javax. servlet. ServletException;
Public class upload {
Private static String newline = "";
Private String uploadDirectory = ".";
Private String ContentType = "";
Private String CharacterEncoding = "";
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;
}
Return s. substring (I + 1 );
}
Public void setUploadDirectory (String s ){
UploadDirectory = 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;
}
Public void uploadFile (HttpServletRequest req) throws ServletException, IOException {
SetCharacterEncoding (req. getCharacterEncoding ());
SetContentType (req. getContentType ());
UploadFile (req. getInputStream ());
}
Public void uploadFile (ServletInputStream servletinputstream) throws ServletException, IOException {
String s5 = null;
String filename = null;
Byte Linebyte [] = new byte [1, 4096];
Byte outLinebyte [] = new byte [4096];
Int ai [] = new int [1];
Int ai1 [] = new int [1];
String line;
// Get the file name
While (line = readLine (Linebyte, ai, servletinputstream, CharacterEncoding ))! = Null ){
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 = line;
If (filename! = Null &&! Filename. equals (""")){
Filename = getFileName (filename );
String sContentType = readLine (Linebyte, ai, servletinputstream, CharacterEncoding );
If (sContentType. indexOf ("Content-Type")> = 0)
ReadLine (Linebyte, ai, servletinputstream, CharacterEncoding );
// File (String parent, String child)
// Creates a new File instance from a parent pathname string
// And a child pathname string.
File file = new File (uploadDirectory, filename );
// FileOutputStream (File file)
// Creates a file output stream to write to the file represented
// By the specified File object.
FileOutputStream fileoutputstream = new FileOutputStream (file );
While (sContentType = readLine (Linebyte, ai, servletinputstream, CharacterEncoding ))! = Null ){
If (sContentType. indexOf (ContentType) = 0 & Linebyte [0] = 45)
Break;
If (s5! = Null ){
// Write (byte [] B, int off, int len)
// Writes len bytes from the specified byte array starting
// At offset off to this file output stream.
Fileoutputstream. write (outLinebyte, 0, ai1 [0]);
Fileoutputstream. flush ();
}
S5 = readLine (outLinebyte, ai1, servletinputstream, CharacterEncoding );
If (s5 = null // s5.indexOf (ContentType) = 0 & outLinebyte [0] = 45)
Break;
Fileoutputstream. write (Linebyte, 0, ai [0]);
Fileoutputstream. flush ();
}
Byte byte0;
If (newline. length () = 1)
Byte0 = 2;
Else
Byte0 = 1;
If (s5! = Null & outLinebyte [0]! = 45 & ai1 [0]> newline. length () * byte0)
Fileoutputstream. write (outLinebyte, 0, ai1 [0]-newline. length () * byte0 );
If (sContentType! = Null & Linebyte [0]! = 45 & ai [0]> newline. length () * byte0)
Fileoutputstream. write (Linebyte, 0, ai [0]-newline. length () * byte0 );
Fileoutputstream. close ();
}
}
Private String readLine (byte Linebyte [], int ai [],
ServletInputStream servletinputstream,
String CharacterEncoding ){
Try {
// ReadLine (byte [] buffer, int offset, int length)
// Reads a line from the POST data.
Ai [0] = servletinputstream. readLine (Linebyte, 0, Linebyte. length );
If (ai [0] =-1)
Return null;
} Catch (IOException _ ex ){
Return null;
}
Try {
If (CharacterEncoding = null ){
// Convert the given byte array to a string using the default encoding method
// String (byte [] bytes, int offset, int length)
Return new String (Linebyte, 0, ai [0]);
} Else {
// Convert the given byte array to a string using the given encoding method
// String (byte [] bytes,