动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
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教程 ]的信息

本月文章推荐
.Struts框架之构建Model组件.
.Java编程经验技巧 谨慎使用Date和.
.Java开源项目——突破JUnit的局限.
.Java设计模式之虚拟代理模式.
.在组件(components)之间共享Tick.
.PowerFolder 工作流服务器版本0..
.J2SE综合:对java.util的总结 八.
.用Flash远程调用增强J2EE表示层.
.深入浅析Tomcat配置技巧Top10.
.Struts控制器组件简单介绍.
.探索研究Laszlo的类、属性及事件.
.Java初学者入门需掌握的30个基本.
.JDBC 连接MySQL 问题.
..NET 和智能传输服务API编写自动.
.如何制作最小的RCP程序压缩包.
.深入浅出Java设计模式之备忘录模.
.WirelessMessagingAPI(1).
.J2EE综合-浅析Java程序员的存储过.
.构建器内部的多形性方法的行为.
.Java认证经典模拟题.

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递归程序的优化 人气:586
下一篇:Turbine Development Kit HowTo 中文 人气:514
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-8-30 HBcms(宏博cms)内容管理系统 v1.1
2008-8-30 邓西网站帮助系统 v2.3
2008-8-30 破竹CMS v4.0.7.21
2008-8-30 美女写真网图片小偷 v1.5 全站静
2008-8-30 PHP美女写真Cms v1.00
2008-8-30 PHPer.yang情书系统 v1.0
2008-8-30 快乐商城全站源码
2008-8-29 oblog v4.6 build 20080827
2008-8-29 ASBLOG v2.5 bulid 080828
2008-8-23 Mini WinMount V0.4
2008-8-23 Vista优化大师3.11正式版
2008-8-23 Wine 1.13
2008-8-23 KlipFolio 5.0 Build 5899-80
2008-8-23 Windows Sysinternals Desktops
2008-8-23 OneTap Movies1.2破解版
2008-8-23 AnnotaterPDF阅读1.1.503 破解版
2008-8-23 SoundMeter分贝测量仪 v1.0汉化破
2008-8-23 iDrum音乐节拍1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | Html转换js | js/vbs加密 | md5加密 | 进制转换
实用工具:汉字翻译拼音 | 符号对照表 | 个税计算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号