网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.再谈面向对象.
.Java移动设备D图形:M3G快速模式(.
.JNDI查找示例.
.使用 EJB 3.0 Java Persistence .
.C与Java在流程控制上的异同.
.诊断Java代码:设计“可测试的”.
.EJB的编程限制.
.Java中文问题详解(推荐).
.Java新手常犯的几种错误.
.Java开源实践(1)--环境配置.
.里程碑式的革新—全新J2SE 1.5新.
.Java实时应用程序中的内存管理.
.把业务流程从操作中剥离出来.
.Java应用性能的提升.
.使用J2ME技术实现简单的动画(附代.
.百分之百纯 Java(TM)--名词解释.
.Java语言与Generics.
.实用Script详解:一触即停的滚动.
.后向引用.
.程序员的七大武器.

UploadBean源代码

发表日期:2008-1-5



  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]==´
上一篇:TSP递归程序的优化 人气:842
下一篇:Turbine Development Kit HowTo 中文 人气:708
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐