用JSP編寫文件上傳
更新時(shí)間:2006年10月13日 00:00:00 作者:
如果你曾用VB編寫文件上傳的組件的話,那么用JAVA編寫文件上傳的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("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);
}
如果有不明白的發(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("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);
}
如果有不明白的發(fā)E-Mail:yymailbox@263.net.Bye
}
您可能感興趣的文章:
- Jsp頁(yè)面實(shí)現(xiàn)文件上傳下載類代碼
- JSP上傳文件到指定位置實(shí)例代碼
- jsp中點(diǎn)擊圖片彈出文件上傳界面及預(yù)覽功能的實(shí)現(xiàn)
- jsp實(shí)現(xiàn)文件上傳下載的程序示例
- AJAX和JSP實(shí)現(xiàn)的基于WEB的文件上傳的進(jìn)度控制代碼
- jsp文件上傳與下載實(shí)例代碼
- jsp 文件上傳瀏覽,支持ie6,ie7,ie8
- jsp+ajax實(shí)現(xiàn)無(wú)刷新上傳文件的方法
- JSP實(shí)現(xiàn)快速上傳文件的方法
- JSP實(shí)用教程之簡(jiǎn)易文件上傳組件的實(shí)現(xiàn)方法(附源碼)
相關(guān)文章
Java數(shù)據(jù)對(duì)象JDO 2.0查詢語(yǔ)言的特點(diǎn)
這篇文章主要介紹了Java數(shù)據(jù)對(duì)象JDO 2.0查詢語(yǔ)言的特點(diǎn)2006-10-10
jsp中Action使用session方法實(shí)例分析
這篇文章主要介紹了jsp中Action使用session方法,實(shí)例分析了action操作session的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
jsp頁(yè)面調(diào)用applet實(shí)現(xiàn)人民幣的大小寫轉(zhuǎn)換
jsp頁(yè)面調(diào)用applet實(shí)現(xiàn)人民幣的大小寫轉(zhuǎn)換...2006-10-10
JSP session配置對(duì)web應(yīng)用的影響
這篇文章主要介紹了JSP session配置對(duì)web應(yīng)用的影響的相關(guān)資料,需要的朋友可以參考下2017-05-05
基于JSP的動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)技術(shù)
基于JSP的動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)技術(shù)...2006-10-10
JSP生成靜態(tài)頁(yè)實(shí)踐及其設(shè)計(jì)思想[轉(zhuǎn)]
JSP生成靜態(tài)頁(yè)實(shí)踐及其設(shè)計(jì)思想[轉(zhuǎn)]...2007-01-01

