关于截上传文件流

 package servlet;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class File extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public File() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        char[] hchl={13,10}; //回车换行符
        String boundary=request.getContentType().substring(30);
        System.out.println("分界:"+boundary);
        String field_boundary="--"+boundary+new String(hchl);//开始时的分隔符
        String last_boundary="--"+boundary+"--"+new String(hchl);//结束时的分隔符
        String _msg="";
        ServletInputStream getdata=request.getInputStream();
        ByteArrayOutputStream temp=new ByteArrayOutputStream();
        byte[] data_line=new byte[1024];
        int line_byte_count=0;
        boolean found_boundary=false; //是否读完的标记
        while((line_byte_count=getdata.readLine(data_line,0,data_line.length))!=-1){
            if(!found_boundary){
                line_byte_count=getdata.readLine(data_line,0,data_line.length);
            }
            String temp_str=new String(data_line,0,line_byte_count);
            if(temp_str.indexOf("filename")!=-1){ //找一行中filename的位置,没有则返回-1,在这里是找到有filename的那一行
                if(temp_str.substring(temp_str.indexOf("filename=")+9,temp_str.lastIndexOf("/"")+1).length()>2){
                    String file_name = null;
                    if(temp_str.indexOf("//")!=-1)
                        file_name=temp_str.substring(temp_str.lastIndexOf("//")+1,temp_str.lastIndexOf("/""));
                    else{
                        file_name=temp_str.substring(temp_str.indexOf("filename=")+10, temp_str.lastIndexOf("/""));
                    }
                    line_byte_count=getdata.readLine(data_line,0,data_line.length); //这一行大概是Content-Type:
                    line_byte_count=getdata.readLine(data_line,0,data_line.length); //这一行是空的
                    FileOutputStream myfile=new FileOutputStream("e://xx//upload//"+file_name,false); //文件存放目录
                    boolean test=true;
                    while(test) {
                        line_byte_count=getdata.readLine(data_line,0,data_line.length);
                        if(line_byte_count==-1){
                            test=false;
                            break;
                        }
                        if(temp.size()==0){
                            temp.write(data_line,0,line_byte_count);
                        }else{
                            if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
                                myfile.write(temp.toByteArray(),0,temp.toByteArray().length-2);
                                temp.reset();
                                myfile.close();
                                //out.println(file_name+"上传成功了 <br>");
                                _msg=_msg+file_name+"上传成功<br>";
                                test=false;
                                found_boundary=true;
                            }else{
                                temp.writeTo(myfile);
                                temp.reset();
                                temp.write(data_line,0,line_byte_count);
                            }
                        }
                    }
                }else{
                    String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf(";")-1);
                    line_byte_count=getdata.readLine(data_line,0,data_line.length);
                    line_byte_count=getdata.readLine(data_line,0,data_line.length);
                    line_byte_count=getdata.readLine(data_line,0,data_line.length);
                    line_byte_count=getdata.readLine(data_line,0,data_line.length);
                    found_boundary=true;
                   // out.println(field_name+"没有选择上传文件! <br>");
                   // _msg=_msg+field_name+"没有选择上传文件<br>";
                }
            } else{
                String field_name=temp_str.substring(temp_str.indexOf("name")+6,temp_str.lastIndexOf("/""));
                line_byte_count=getdata.readLine(data_line,0,data_line.length);
                temp.reset();
                boolean test=true;
                while(test) {
                    line_byte_count=getdata.readLine(data_line,0,data_line.length);
                    if(line_byte_count==-1){
                        test=false;
                        break;
                    }
                    if(new String(data_line,0,line_byte_count).equals(field_boundary) || new String(data_line,0,line_byte_count).equals(last_boundary)){
                        test=false;
                        found_boundary=true;
                        if(temp.size()>2){
                           // out.println(field_name+":"+new String(temp.toByteArray())+" <br>");
                           _msg=_msg+field_name+":"+new String(temp.toByteArray())+"<br>";
                        }else{
                           // out.println(field_name+"没有内容! <br>");
                           _msg=_msg+field_name+"没有内容!<br>";
                        }
                        temp.reset();
                    }else{
                        temp.write(data_line,0,line_byte_count);
                    }
                }
            }
            
        }
        getdata.close();
        out.println("<script>window.parent.Finish('"+_msg+"');</script>");
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值