jsp实现文件上传
2006-08-06 13:58
399 查看
写程序嘛~真是TMD的郁闷~之前测试的jsp文件下载 明明在win测试都OK的!但是去了freebsd就出现问题了!-_- 而且N多~ 算了~ 不提了!呵呵!昨天测试了一晚~~把 文件下载的改了改~发现直接用
BufferedInputStream的
int read() 参见
int read(byte[] b, int off, int len)
在此字节输入流中从给定的偏移量开始将各字节读取到指定的 byte 数组中。
这两个方法N大的差别郁闷!...现在给出一个上传的代码片段
void upfile(HttpServletRequest request,javax.servlet.jsp.JspWriter out,String filename)throws Exception{
String boundary = request.getContentType().substring(30);//读取分隔符
ServletInputStream sis=request.getInputStream();
BufferedOutputStream bos=null;
byte[] buffer = new byte[256];
String line=null;
for(int i=0;i<5;i++){
line=readLine(buffer,sis);
}
try{
bos=new BufferedOutputStream(new FileOutputStream(filename));
//以下为读内容
while(line!=null&&line.indexOf(boundary)==-1){
bos.write(buffer,0,line.getBytes().length);
line=readLine(buffer,sis);
}
out.print("upload success!");
}catch(Exception e){
out.print("upload error");
}finally{
if(bos!=null)bos.close();
}
}
String readLine(byte[] lineByte,ServletInputStream servletInputstream){
try{
int len=0;
len=servletInputstream.readLine(lineByte,0,lineByte.length);
if(len == -1){
return null;
}else{
return new String(lineByte,0,len);
}
}catch(Exception _ex){
return null;
}
}
起HTML 代码为
<TR>
<FORM action=?Action=Upfile method=post encType=multipart/form-data>
<TD borderColorLight=black bgColor=menu>file upload</TD>
<TD>file:<INPUT type=file name=file>up to file<INPUT size=35 name=UPaddress></TD>
<TD><INPUT onclick="this.form.action+='&UPaddress='+this.form.UPaddress.value;" type=submit value=upl name=up></TD></FORM></TR>
写jspwebshell 累啊~~~~~~~~~
BufferedInputStream的
int read() 参见
InputStream的
read方法的常规协定。
int read(byte[] b, int off, int len)
在此字节输入流中从给定的偏移量开始将各字节读取到指定的 byte 数组中。
这两个方法N大的差别郁闷!...现在给出一个上传的代码片段
void upfile(HttpServletRequest request,javax.servlet.jsp.JspWriter out,String filename)throws Exception{
String boundary = request.getContentType().substring(30);//读取分隔符
ServletInputStream sis=request.getInputStream();
BufferedOutputStream bos=null;
byte[] buffer = new byte[256];
String line=null;
for(int i=0;i<5;i++){
line=readLine(buffer,sis);
}
try{
bos=new BufferedOutputStream(new FileOutputStream(filename));
//以下为读内容
while(line!=null&&line.indexOf(boundary)==-1){
bos.write(buffer,0,line.getBytes().length);
line=readLine(buffer,sis);
}
out.print("upload success!");
}catch(Exception e){
out.print("upload error");
}finally{
if(bos!=null)bos.close();
}
}
String readLine(byte[] lineByte,ServletInputStream servletInputstream){
try{
int len=0;
len=servletInputstream.readLine(lineByte,0,lineByte.length);
if(len == -1){
return null;
}else{
return new String(lineByte,0,len);
}
}catch(Exception _ex){
return null;
}
}
起HTML 代码为
<TR>
<FORM action=?Action=Upfile method=post encType=multipart/form-data>
<TD borderColorLight=black bgColor=menu>file upload</TD>
<TD>file:<INPUT type=file name=file>up to file<INPUT size=35 name=UPaddress></TD>
<TD><INPUT onclick="this.form.action+='&UPaddress='+this.form.UPaddress.value;" type=submit value=upl name=up></TD></FORM></TR>
写jspwebshell 累啊~~~~~~~~~
相关文章推荐
- JSP实现文件上传——uploadFile.jsp
- jspsmart实现文件上传下载及jspSmartUpload.jar下载
- web 开发之js---巧用iframe实现jsp无刷新上传文件
- jsp实现文件上传
- 用jsp、Servlet实现上传文件
- 用JSP实现上传文件的两种方法
- jsp实现文件上传
- Servlet和Jsp实现原生的上传与下载文件
- JSP用common-fileupload实现文件上传下载
- 使用jsp/servlet简单实现文件上传所遇到的问题
- 【ZT】Jsp/Servlet:实现文件上传与下载【三】
- 浅谈利用jspsmart实现文件上传下载(转)
- jsp中实现一个页面纯io流上传文件
- 研究了三天的文件上传(jspsmart实现)file和text表单同时提交的问题
- 【转载】使用jsp实现文件上传到服务器或者从服务器上下载文件到本地计算机完整说明版
- JSP + Servlet 实现的文件上传到服务器的功能
- 利用jsp+Extjs实现动态显示文件上传进度
- 用JspSmart组件实现文件上传和下载
- JSP上传文件的实现,应用jspsmart组件包
- Jsp+Servlet实现文件上传下载 文件上传(一)