帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > JAVA编程 >
UploadBean源代码
作者:未知 发布时间:2005-03-12 来源:JSP天空网
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]==&acute; &acute;)
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]==&acute;-&acute;)
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]==&acute; &acute; && m_binaries[m_currentIndex+2]==&acute; &acute;){
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(&acute;/&acute;)+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(&acute;"&acute;)+1)==filename.lastIndexOf(&acute;"&acute;))
filename="";
else filename=filename.substring(filename.indexOf(&acute;"&acute;)+1,filename.lastIndexOf(&acute;"&acute;));
return filename;
}
public void setMAXFILESIZE(int maxValue){
if(maxValue>0)
this.MAX_FILE_SIZE=maxValue;
}
  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·放眼未来 帝国近期将有重大举措!
·PHPWind6.3.2版通行证发布
·帝国备份王2008版正式发布
·帝国备份王2008版发布
·phpcms2007转帝国CMS5.0程序发布
·dedecms5.1转帝国CMS5.0程序发布
·帝国网站管理系统V5.0商业购买说明
   类别最新
·谈谈JDBC
·JDBC专题介绍
·JDBC接口技术
·利用weblogic的POOL(连接池)连接
·Jsp中调用Oracle存储过程的小例子
·JSP数据库操作例程
·JSP数据库连接大全
·用连接池提高Servlet访问数据库的效
·一种简单JDBC连接池的实现
·数据库连接池Java实现小结
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统