网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  uploadbean源代码     
  文章作者:未知  文章来源:水木森林  
  查看:138次  录入:管理员--2007-11-17  
 
  import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class uploadbean {
servletrequest request;
servletinputstream input;
string objectdir="c:/upload/";
private int m_currentindex;
private int max_file_size=50*1024*1024;
private byte[] m_binaries;
private string m_boundary;
private int contentlength;
private string filename="";

public uploadbean(){
super();
m_currentindex=0;
}
public uploadbean(servletrequest request){
this();
this.setrequest(request);
}
public void setrequest(servletrequest request){
if (request!=null){
this.request=request;
try{
this.setinputstream(request.getinputstream());
}catch(ioexception ioe){
system.out.println("ioexception occurred in com.javacat.jsp.beans.upload.uploadbean.setrequest:"+ioe.getmessage());
}
}
}

public servletrequest getrequest(){
return this.request;
}

public void setinputstream(servletinputstream inputstream){
this.input=inputstream;
}

public servletinputstream getinputstream(){
return this.input;
}

public outputstream getoutputstream(string filename) throws ioexception{
int findex=filename.lastindexof("/");
file file=new file(getobjectdir(), filename.substring(findex+1));
this.filename=filename.substring(findex+1);
return new fileoutputstream(file);
}

public void setobjectdir(string dir){
this.objectdir=dir;
}
public string getobjectdir(){
return this.objectdir;
}
public string getfilename(){
return this.filename;
}
public int upload() throws ioexception,securityexception{
if(request==null)
return -2;
boolean isfile;
string dataheader;
string filename="";
byte[] thebytes;
int countfile=0;
outputstream output;
contentlength =request.getcontentlength();

m_binaries=new byte[contentlength];
int haveread=0;
while (haveread<contentlength) {
haveread+=getinputstream().read(m_binaries, haveread, contentlength-haveread);
}
//system.out.println("content= "+new string(m_binaries));
boolean match=false;
m_boundary=new string();
for (; !match && m_currentindex<contentlength; m_currentindex++ ){
if(m_binaries[m_currentindex]==´´)
match=true;
else
m_boundary=m_boundary+(char)m_binaries[m_currentindex];
}
if (m_currentindex==1)
return -1;
m_currentindex++;
do{
if(m_currentindex>=contentlength)
break;
dataheader=getdataheader();
m_currentindex=m_currentindex+2;
isfile=dataheader.indexof("filename")>0;
if (isfile) {
filename="";
if(!getfilepath(dataheader).equals(""))
filename=getfilename(getfilepath(dataheader));
if((filename==null)||(filename.equals("")))
isfile=false;
}
thebytes=getdatasection();
if(isfile){
if(thebytes.length>this.max_file_size)
throw new securityexception("file size is too large.10m bytes is a limited");
else{
output=getoutputstream(filename);
output.write(thebytes);
countfile++;
output.close();
}
}
if ((char)m_binaries[m_currentindex+1]==´-´)
break;
m_currentindex=m_currentindex+2;
}while(true);
return countfile;
}

private byte[] getdatasection(){
int searchposition=m_currentindex;
int keyposition=0;
int boundarylen=m_boundary.length();
int start=m_currentindex;
int end=m_currentindex;
do{
if(searchposition>=contentlength )
break;
if(m_binaries[searchposition]==(byte)m_boundary.charat(keyposition)){
if(keyposition==boundarylen-1){
end=searchposition - boundarylen - 2;
break;
}
searchposition++;
keyposition++;
}
else{
searchposition++;
keyposition=0;
}
}while(true);
m_currentindex=end+boundarylen+3;
byte[] data=new byte[end-start+1];
for(int i=0; i<data.length; i++)
data[i]=m_binaries[start+i];
return data;
}

private string getdataheader(){
int start =m_currentindex;
int end=0;
int len=0;
boolean match=false;
while(!match){
if(m_binaries[m_currentindex]==´´ && m_binaries[m_currentindex+2]==´´){
match=true;
end=m_currentindex-1;
m_currentindex=m_currentindex+2;
}
else{
m_currentindex++;
}
}
return new string(m_binaries,start, (end- start )+1);
}

private string getfilename(string filepathname){
int pos=-1;
pos=filepathname.lastindexof(´/´)+1;
if(pos>0)
return filepathname.substring(pos, filepathname.length());
else
return filepathname;
}
private string getfilepath(string header){
int filenamestart=header.indexof("filename=");
int ctypeindex=header.indexof("content-type");
string filename=header.substring(filenamestart, ctypeindex);
if ((filename.indexof(´"´)+1)==filename.lastindexof(´"´))
filename="";
else filename=filename.substring(filename.indexof(´"´)+1,filename.lastindexof(´"´));
return filename;
}
public void setmaxfilesize(int maxvalue){
if(maxvalue>0)
this.max_file_size=maxvalue;
}
}
 
 
上一篇: turbine development kit howto 中文    下一篇: visualcafe的主要工具介绍
  相关文档
java编程经验技巧 谨慎使用date和time类 03-31
垃圾清理势在必行——java垃圾收集算法 11-17
基础:java/j2ee中文问题终极解决之道 02-28
jsr 198最终草案提交 统一ide插件标准 11-17
用 websphere studio 创建 jsf 提交表单(4) 11-17
java涉及到的开发工具与开发环境问答集 11-30
关于扰乱器与反编译 11-17
java & regex(2-3) 11-17
防止到string类的不恰当的类型转换 11-17
使用j2seapi读取properties文件的六种方法 11-17
test of the java skill(3) 11-16
对 robocode 的创始人 mat nelson 的一次采访 11-17
jgraph 5.8 发布 11-17
jdk1.4的正则表达式使用介绍 11-17
jbuilder2005 struts深度体验之新增 11-17
java新手入门——整理的scjp听课笔记 11-17
敏捷开发的必要技巧:移除重复代码 11-17
java socket编程(三)服务器sockets 11-16
java学习,是一条漫长的道路(转贴) 11-17
java基础篇 初学者入门必备的学习资料 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息