Using JSP to write file upload program

Source: Internet
Author: User
Tags config file upload return string
js| Program | upload
If you have used VB to write the components of the file upload, then the Java file Upload JavaBean is very easy.
The following example is just a simple 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\\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 there is not clear to the forum to express their views

}


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.