Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>EJB技术>>一个文件上传JavaBean
一个文件上传JavaBean
2005-06-08   来源:未知  作者:未知
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 = ""; 
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){ 
try{ 
//readLine(byte[] buffer, int offset, int length) 
//Reads a line from the POST data. 
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length); 
if(ai[0] == -1) 
return null; 
}catch(IOException _ex){ 
return null; 

try{ 
if(CharacterEncoding == null){ 
//用缺省的编码方式把给定的byte数组转换为字符串 
//String(byte[] bytes, int offset, int length) 
return new String(Linebyte, 0, ai[0]); 
}else{ 
//用给定的编码方式把给定的byte数组转换为字符串 
//String(byte[] bytes, int offset, int length, String enc) 
return new String(Linebyte, 0, ai[0], CharacterEncoding); 

}catch(Exception _ex){ 
return null; 


/* 
public int readLine(byte[] buffer, 
int offset, 
int length) throws java.io.IOException 
从POST来的数据中读一行 
参数: 
buffer - buffer to hold the line data 
offset - offset into the buffer to start 
length - maximum number of bytes to read. 
Returns: 
number of bytes read or -1 on the end of line. 
*/ 



upload.jsp 
<%@page import="com.upload.upload"%> 
<% 
String Dir = "/home/tonywan/upload"; 

upload upload = new upload(); 
upload.setUploadDirectory(Dir); 
upload.uploadFile(request); 
out.print("<center><font color=red>成功上载文件至" + Dir + "</font></center>"); 
%> 


upload.html 
<center>File upload test</center> 


<table><tr> 
<form method="post" enctype="multipart/form-data" action="upload.jsp"> 
<td> <input type=file size=20 name="fname"> </td> 
<td> <input type=Submit value=Upload> </td> </form> 
</tr></table> 
  --相关文章--
· 配置WebLogic Server集群二(组图) (2007-04-17)
· 配置WebLogic Server集群一(组图) (2007-04-17)
· 程序员应用EJB 3.0必要的准备 (2007-04-17)
· 用EJB 3.0开发企业级Bean组件初体验 (2007-04-17)
· 漫谈EJB在Java中的应用 (2007-04-17)
· 深入探究EJB应用技术的体系结构 (2007-04-17)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接