动态网站制作指南
[  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,迁移,结构,破解,编译,配置,进程
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息

本月文章推荐
.JCreatorPro3.0与WTK2.0配置心得.
.行列式算法(java).
.Java程序员就业前景.
.用 Apache JMeter 测试 WebSpher.
.Java模式设计之单例模式(四).
.Java学习之踏上旅途的第一步.
.优化JDBC性能的三大技巧.
.J2EE中使用Display标记库来展示表.
.等级结构数据集在Aspire和Tomcat.
.搭建Linux下Java开发环境.
.Swing读书笔记.
.用jscript对表单数据进行utf-8编.
.定时执行任务的三种方法.
.用J2SE 1.4进行Internet安全编程.
.4类Java技术平行发展 Sun公司计划.
.random 方法.
.java实现屏幕取色.
.Java Socket编程中的一个秘密类.
.实现Java与C语言接口步骤.
.程序人生:Java三个月—OOP的乐趣.

一个文件上传JAVABEAN

文章类别:Java教程 | 发表日期:2008-1-5 |


 


package  com.upload;  

import  Java.io.*;  
import  javax.servlet.http.HttpServletRequest;  
import  javax.servlet.ServletInputStream;  
import  javax.servlet.ServletException;  

public  class  upload{  
private  static  String  newline  =  "\n";  
private  String  uploadDirectory  =  ".";  
private  String  ContentType  =  "";  
private  String  CharacterEncoding  =  "";  

private  String  getFileName(String  s){  
int  i  =  s.lastIndexOf("\\");  
if(i  <  0  //  i  >=  s.length()  -  1){  
i  =  s.lastIndexOf("/");  
if(i  <  0  //  i  >=  s.length()  -  1)  
return  s;  
}  
return  s.substring(i  +  1);  
}  

public  void  setUploadDirectory(String  s){  
uploadDirectory  =  s;  
}  

public  void  setContentType(String  s){  
ContentType  =  s;  
int  j;  
if((j  =  ContentType.indexOf("boundary="))  !=  -1){  
ContentType  =  ContentType.substring(j  +  9);  
ContentType  =  "--"  +  ContentType;  
}  
}  

public  void  setCharacterEncoding(String  s){  
CharacterEncoding  =  s;  
}  

public  void  uploadFile(  HttpServletRequest  req)  throws  ServletException,  IOException{  
setCharacterEncoding(req.getCharacterEncoding());  
setContentType(req.getContentType());  
uploadFile(req.getInputStream());  
}  

public  void  uploadFile(  ServletInputStream  servletinputstream)  throws  ServletException,  IOException{  

String  s5  =  null;  
String  filename  =  null;  
byte  Linebyte[]  =  new  byte[4096];  
byte  outLinebyte[]  =  new  byte[4096];  
int  ai[]  =  new  int[1];  
int  ai1[]  =  new  int[1];  

String  line;  
//得到文件名  
while((line  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding))  !=  null){  
int  i  =  line.indexOf("filename=");  
if(i  >=  0){  
line  =  line.substring(i  +  10);  
if((i  =  line.indexOf("\""))  >  0)  
line  =  line.substring(0,  i);  
break;  
}  
}  

filename  =  line;  

if(filename  !=  null  &&  !filename.equals("\"")){  
filename  =  getFileName(filename);  

String  sContentType  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding);  
if(sContentType.indexOf("Content-Type")  >=  0)  
readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding);  

//File(String  parent,  String  child)  
//Creates  a  new  File  instance  from  a  parent  pathname  string  
//and  a  child  pathname  string.  
File  file  =  new  File(uploadDirectory,  filename);  

//FileOutputStream(File  file)  
//Creates  a  file  output  stream  to  write  to  the  file  represented  
//by  the  specified  File  object.  
FileOutputStream  fileoutputstream  =  new  FileOutputStream(file);  

while((sContentType  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding))  !=  null){  
if(sContentType.indexOf(ContentType)  ==  0  &&  Linebyte[0]  ==  45)  
break;  

if(s5  !=  null){  
//write(byte[]  b,  int  off,  int  len)  
//Writes  len  bytes  from  the  specified  byte  array  starting  
//at  offset  off  to  this  file  output  stream.  
fileoutputstream.write(outLinebyte,  0,  ai1[0]);  
fileoutputstream.flush();  
}  
s5  =  readLine(outLinebyte,  ai1,  servletinputstream,  CharacterEncoding);  
if(s5  ==  null  //  s5.indexOf(ContentType)  ==  0  &&  outLinebyte[0]  ==  45)  
break;  
fileoutputstream.write(Linebyte,  0,  ai[0]);  
fileoutputstream.flush();  
}  

byte  byte0;  
if(newline.length()  ==  1)  
byte0  =  2;  
else  
byte0  =  1;  
if(s5  !=  null  &&  outLinebyte[0]  !=  45  &&  ai1[0]  >  newline.length()  *  byte0)  
fileoutputstream.write(outLinebyte,  0,  ai1[0]  -  newline.length()  *  byte0);  
if(sContentType  !=  null  &&  Linebyte[0]  !=  45  &&  ai[0]  >  newline.length()  *  byte0)  
fileoutputstream.write(Linebyte,  0,  ai[0]  -  newline.length()  *  byte0);  

fileoutputstream.close();  
}  
}  

private  String  readLine(byte  Linebyte[],  int  ai[],  
ServletInputStream  servletinputstream,  
String  CharacterEncoding){  



上一篇:JavaBean入门 人气:259
下一篇:多线程学习初步 人气:297
点击此处浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-5-16 乘风多用户PHP统计系统 v3.4
2008-5-16 轩溪下载系统 v3.78 build 0515
2008-5-16 普沙B2B 浙江省商贸网 v2.0
2008-5-16 asp抓蜘蛛的小程序 v1.0
2008-5-16 齐齐乐网私服发布站 仿haosf新版
2008-5-16 IssTech信息反馈系统 v1.0
2008-5-16 自由领域大头贴(js接口版) 修正版
2008-5-16 医院网站系统
2008-5-16 智拓-分类信息管理系统 v5.0
2008-5-7 Windows XP SP3 官方英文版
2008-5-7 Windows XP SP3 官方香港中文版
2008-5-7 Windows XP SP3 官方繁体中文版
2008-5-7 Windows XP SP3 官方简体中文版
2008-4-30 Multiple Unzip Wizard 1.02
2008-4-30 Multiple Unrar Wizard 1.0.0
2008-4-30 WinZip Install/Try/Uninstall a
2008-4-30 ZIP压缩文件修复器WzipFix 2.0
2008-4-30 Pentazip 6.01 Build 189 For Wi
  发表评论
姓 名: 验证码: [ 全部贴吧 ] [ 浏览评论 ]
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号