如何用JSP編寫檔案上傳

來源:互聯網
上載者:User

 

如果你曾用VB編寫檔案上傳的組件的話,那麼用JAVA編寫檔案上傳的JAVABEAN十分容易。

下面的例子只是一個簡版

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("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<img.length;i++)

{ os.write(img[i]); }

os.close();

}

catch(IOException e)

{return(false);}

return(true);

}

如果有不明白的發E-Mail:yymailbox@263.net.Bye

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.