电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > javascript > 正文
用JSP编写文件上传
用JSP编写文件上传
2005-12-30 19:19:26  文/45IT收集   出处:电脑软硬件应用网   


如果你曾用VB编写文件上传的组件的话,那么用JAVA编写文件上传的JAVABEAN十分容易。  
下面的例子只是一个简版  
package yuanyifileup;  
import java.io.*;  
import java.util.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
import javax.servlet.jsp.PageContext;  

public class yuanyifileup  
{  
private ServletRequest request;  
private ServletResponse response;  
private ServletConfig config;  
ServletInputStream DATA;  
int FormSize;  
File f1;  
FileOutputStream os;  
DataInputStream is;  
String filename;  
byte[] b;  
byte t;  
boolean flag=false;  
public yuanyifileup()  
{ }  
public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException  
{  
this.request=request;  
this.response=response;  
this.config=config;  
DATA = request.getInputStream();  
FormSize=request.getContentLength();  
}  
public void initialize(PageContext pageContext) throws IOException  
{  
request=pageContext.getRequest();  
response=pageContext.getResponse();  
config=pageContext.getServletConfig();  
DATA = request.getInputStream();  
FormSize=request.getContentLength();  
}  
public boolean setFilename(String s)  
{  
try  
{  
File f1=new File(s);  
os=new FileOutputStream(f1);  
}  
catch(IOException e)  
{return(false);}  
return(true);  
}  
public void getByte()  
{  
int i=0;  
try  
{  
is=new DataInputStream(DATA);  
b=new byte[FormSize];  

while (true)  
{  
try  
{  
t=is.readByte();  
b[i]=t;  
i++;  
}  
catch(EOFException e)  
{ break;}  
}  
is.close();}  
catch(IOException e)  
{}  
}  

public boolean save()  
{  
int i=0,start1=0,start2=0;  
String temp=\"\";  
if (!flag)  
{  
getByte();  
flag=true;  
}  
try  
{  
temp=new String(b,\"ISO8859_1\");  
}  
catch(UnsupportedEncodingException e)  
{return(false);}  

start1=temp.indexOf(\"image/\");  
temp=temp.substring(start1);  

start1=temp.indexOf(\"\\r\\n\\r\\n\");  

temp=temp.substring(start1+4);  
start2=temp.indexOf(\";\\r\\n\");  
if (start2!=-1)  
{  
temp=temp.substring(0,start2);  
}  
try  
{  
byte[] img=temp.getBytes(\"ISO8859_1\");  
for (i=0;i<img.length;i++)  
{ os.write(img[i]); }  
os.close();  
}  
catch(IOException e)  
{return(false);}  

return(true);  

}  
如果有不明白的发E-Mail:yymailbox@263.net.Bye  

}

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    javascript实用技巧点滴(三)
    javascript实用技巧点滴(2)
    javascript实用技巧点滴(1)
    javascript教程 - 第一课 1.1
    用js制作完善的日,月组合下拉框
    利用JSP编程建立动态Web站点
    用PHP制作留言板
    PHP生成动态WAP页面
    PHP实现文件下载
    PHP4的session功能
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号