JSP File Upload

Source: Internet
Author: User
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>

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.