您的位置:首页 > Web前端 > JavaScript

jsp实现文件上传

2006-08-06 13:58 399 查看
   写程序嘛~真是TMD的郁闷~之前测试的jsp文件下载 明明在win测试都OK的!但是去了freebsd就出现问题了!-_- 而且N多~ 算了~ 不提了!呵呵!昨天测试了一晚~~把 文件下载的改了改~发现直接用

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 累啊~~~~~~~~~

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息