Upload. jsp
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<HTML>
<Head>
<Title> upload </title>
</Head>
<Body bgcolor = "# ffffff">
<Form name = "form1" method = "Post" Action = "receive. jsp" enctype = "multipart/form-Data"> Select Upload file:
<Input type = "file" name = "file1" size = "50" id = "myfile">
<Br>
<Input type = "Submit" value = "Upload">
</Form>
</Body>
</Html>
Receive. jsp
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Page import = "java.net. *" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Head>
<Title>
Receieve
</Title>
</Head>
<Body bgcolor = "# ffffff">
<Br>
<%
String tempfile = new string ("mytempfile ");
File temp = new file ("D:" + file. separator, tempfile );
Fileoutputstream o = new fileoutputstream (temp );
Inputstream filesource = NULL;
Filesource = request. getinputstream ();
Byte [] B = new byte [8192];
Int N;
While (n = filesource. Read (B ))! =-1 ){
O. Write (B, 0, N );
O. Flush ();
}
Filesource. Close ();
O. Close ();
Randomaccessfile Rf = new randomaccessfile (temp, "R ");
RF. Readline ();
String filepath = RF. Readline ();
Int position = filepath. lastindexof ('//');
String filename = new string (filepath. substring (Position + 1, filepath. Length ()-1). getbytes ("ISO-8859-1 "),
UTF-8 ");
RF. Seek (0 );
Long forthenterposition = 0;
Int forth = 1;
While (n = RF. readbyte ())! =-1 & (forth <= 4 )){
If (n = 10 ){
Forthenterposition = RF. getfilepointer ();
Forth ++;
}
}
File fileuploaddir = new file ("D:" + file. separator, "Upload ");
Fileuploaddir. mkdir ();
File SaveFile = new file ("D:" + file. Separator + "Upload", filename );
Randomaccessfile rf2 = new randomaccessfile (SaveFile, "RW ");
RF. Seek (RF. Length ());
Long endposition = RF. getfilepointer ();
Int J = 1;
While (endposition> = 0) & (j <= 2 )){
Endposition --;
RF. Seek (endposition );
If (RF. readbyte () = 10)
J ++;
}
RF. Seek (forthenterposition );
Long startpoint = RF. getfilepointer ();
While (startpoint + 8192l <= endPosition-1 ){
N = RF. Read (B );
Startpoint = RF. getfilepointer ();
Rf2.write (B, 0, N );
}
While (startpoint <endPosition-1 ){
Rf2.write (RF. readbyte ());
Startpoint = RF. getfilepointer ();
}
Rf2.close ();
RF. Close ();
Temp. Delete ();
Out. println ("file:" + filename + "succeed to upload! ");
%>
</Html>