网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
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,移动开发
本月文章推荐
.Eclipse快速上手指南之使用CVS(.
.BuildPath 方法.
.使用MIDP2.0开发游戏(7)设计Sche.
.命名你的线程和查看系统.
.Java入门及faq__1(3).
.使用Mascot Capsule Micro3D V3 .
.Swing指南:Spinner Model Contro.
.如何用JDO开发数据库应用.
.在Eclipse中开发struts应用程序.
.[引用] Eclipse 照亮Java众生.
.J2EE编程起步(1).
.如何在Linux平台下使用JNI提高Ja.
.转-关于多线程.
.GetFolder 方法.
.使用Java Annotations来管理对象.
.Java动画程序的设计原理.
.Java中的浮点数分析.
.开源的持续集成框架 CruiseContr.
.hasOwnProperty 方法.
.JavaVM,反射与动态代理.

一个文件上传JAVABEAN

发表日期: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入门 人气:659
下一篇:多线程学习初步 人气:786
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐