Compile File Upload Using jsp

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 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 ("\ r \ n ");

Temp = temp. substring (start1 + 4 );
Start2 = temp. indexOf ("; \ r \ n ");
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.