用jsp編寫(xiě)文件上載
更新時(shí)間:2006年10月13日 00:00:00 作者:
如果你曾用VB編寫(xiě)文件上傳的組件的話,那么用JAVA編寫(xiě)文件上傳的JAVABEAN十分容易。
下面的例子只是一個(gè)簡(jiǎn)版
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<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(IOException e)
{return(false);}
return(true);
}
如果有不明白的發(fā)E-Mail:yymailbox@263.net.Bye
}
下面的例子只是一個(gè)簡(jiǎn)版
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<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(IOException e)
{return(false);}
return(true);
}
如果有不明白的發(fā)E-Mail:yymailbox@263.net.Bye
}
相關(guān)文章
配置Web應(yīng)用環(huán)境實(shí)現(xiàn)JSP留言簿
配置Web應(yīng)用環(huán)境實(shí)現(xiàn)JSP留言簿...2006-10-10
jsp基于XML實(shí)現(xiàn)用戶(hù)登錄與注冊(cè)的實(shí)例解析(附源碼)
這篇文章主要介紹了jsp基于XML實(shí)現(xiàn)用戶(hù)登錄與注冊(cè)的實(shí)例解析,xml做數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶(hù)登錄與注冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-11-11
通用大型網(wǎng)站頁(yè)面靜態(tài)化解決方案
在開(kāi)發(fā)大型網(wǎng)站時(shí),避免不了處理大量的頁(yè)面靜態(tài)化操作,這樣方便加快網(wǎng)站訪問(wèn)速度與流量分流,那么如何來(lái)實(shí)現(xiàn)呢?其實(shí)說(shuō)白了比較簡(jiǎn)單,網(wǎng)站靜態(tài)化主要包括以下幾方面的工作2008-10-10
JSP生成靜態(tài)頁(yè)實(shí)踐及其設(shè)計(jì)思想[轉(zhuǎn)]
JSP生成靜態(tài)頁(yè)實(shí)踐及其設(shè)計(jì)思想[轉(zhuǎn)]...2007-01-01

