Use JSP to write and upload files

Source: Internet
Author: User

If you have used VB To Write File Upload components, it is very easy to use Java to Write File Upload JavaBean.
The following example is a simplified version.
Package yuanyifileup;
Import java. Io .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import javax. servlet. jsp. pagecontext;

Public class yuanyifileup
{
Private servletrequest request;
Private servletresponse response;
Private servletconfig config;
Servletinputstream data;
Int formsize;
File F1;
Fileoutputstream OS;
Datainputstream is;
String filename;
Byte [] B;
Byte T;
Boolean flag = false;
Public yuanyifileup ()
{}
Public void initialize (servletconfig config, httpservletrequest request, httpservletresponse response) throws ioexception
{
This. Request = request;
This. Response = response;
This. Config = config;
Data = request. getinputstream ();
Formsize = request. getcontentlength ();
}
Public void initialize (pagecontext) throws ioexception
{
Request = pagecontext. getrequest ();
Response = pagecontext. getresponse ();
Config = pagecontext. getservletconfig ();
Data = request. getinputstream ();
Formsize = request. getcontentlength ();
}
Public Boolean setfilename (string S)
{
Try
{
File F1 = new file (s );
OS = new fileoutputstream (F1 );
}
Catch (ioexception E)
{Return (false );}
Return (true );
}
Public void getbyte ()
{
Int I = 0;
Try
{
Is = new datainputstream (data );
B = new byte [formsize];

While (true)
{
Try
{
T = is. readbyte ();
B [I] = T;
I ++;
}
Catch (eofexception E)
{Break ;}
}
Is. Close ();}
Catch (ioexception E)
{}
}

Public Boolean save ()
{
Int I = 0, start1 = 0, start2 = 0;
String temp = "";
If (! Flag)
{
Getbyte ();
Flag = true;
}
Try
{
Temp = new string (B, "iso8859_1 ");
}
Catch (unsupportedencodingexception E)
{Return (false );}

Start1 = temp. indexof ("image /");
Temp = temp. substring (start1 );

Start1 = temp. indexof ("rnrn ");

Temp = temp. substring (start1 + 4 );
Start2 = temp. indexof ("; RN ");
If (start2! =-1)
{
Temp = temp. substring (0, start2 );
}
Try
{
Byte [] IMG = temp. getbytes ("iso8859_1 ");
For (I = 0; I {OS. Write (IMG [I]);}
OS. Close ();
}
Catch (ioexception E)
{Return (false );}

Return (true );

}
If you do not understand the E-mail: yymailbox@263.net.Bye

}

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.