当前位置:  技术问答>java相关

100分交换一个文件上传的.class....

    来源: 互联网  发布时间:2015-05-15

    本文导语:  100分交换一个文件上传的.class.... | 一个例子!! import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class UploadFileServlet extends HttpServlet {     public void doPost(HttpServ...

100分交换一个文件上传的.class....

|
一个例子!!

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class UploadFileServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req,
                      HttpServletResponse res)
        throws IOException, ServletException{
        final int NONE=0;
        final int DATAHEADER=1;
        final int FILEDATA=2;
        final int FIELDDATA=3;
        int totalbytes=req.getContentLength();
        byte[] b=new byte[totalbytes];
        String contentType=req.getContentType();
        String fieldname="";
        String fieldvalue="";
        String filename="";
        String boundary="";
        String lastboundary="";
        int fileSize=0;
        Hashtable formfields=new Hashtable();
        int pos =contentType.indexOf("boundary=");
        String fileID;
        if(pos!=-1){
            pos+="boundary=".length();
            boundary="--"+contentType.substring(pos);
            lastboundary=boundary+"--";
        }
        int state=NONE;
        DataInputStream in=new DataInputStream(req.getInputStream());
        in.readFully(b);
        in.close();
        String reqcontent=new String(b);
        BufferedReader reqbuf=new BufferedReader(new StringReader(reqcontent));
        boolean flag=true;
        int i=0;
        while(flag==true){
            String s=reqbuf.readLine();                             
            if(s==lastboundary||s==null)break;
            switch(state){
                case NONE:
                    if(s.startsWith(boundary)){
                    state=DATAHEADER;
                    i+=1;
                    }
                case DATAHEADER:
                    pos=s.indexOf("filename=");
                    if(pos==-1){
                        pos=s.indexOf("name=");
                        pos+="name=".length()+1;
                        s=s.substring(pos);
                        int l=s.length();
                        s=s.substring(0,l-1);
                        fieldname=s;
                        state=FIELDDATA;
                    }
                    else{
                        String temp=s;
                        pos=s.indexOf("filename=");
                        pos+="filename=".length()+1;
                        s=s.substring(pos);
                        int l=s.length();
                        s=s.substring(0,l-1);
                        pos=s.lastIndexOf("\");
                        s=s.substring(pos+1);
                        filename=s;
                        pos=byteIndexOf(b,temp,0);
                        b=subBytes(b,pos+temp.getBytes().length+2,b.length);
                        s=reqbuf.readLine();
                        File f=new File(filename);
                        DataOutputStream fileout=new DataOutputStream(new FileOutputStream(f));
                        b=subBytes(b,s.getBytes().length+4,b.length);
                        pos=byteIndexOf(b,boundary,0);
                        b=subBytes(b,0,pos-1);
                        fileout.write(b,0,b.length-1);
                        fileSize=b.length-1;
                        state=FILEDATA; 
                    }
                    break;
                case FIELDDATA:
                    s=reqbuf.readLine();
                    fieldvalue=s;
                    formfields.put(fieldname,fieldvalue);
                    state=NONE;
                    break;
                case FILEDATA:
                    while((!s.startsWith(boundary))&&(!s.startsWith(lastboundary))){
                        s=reqbuf.readLine();
                        if(s.startsWith(boundary)){
                            state=DATAHEADER;
                        }
                        else break;
                        break;
                    }
                }
            }
            res.setContentType("text/html;charset=gb2312");
            PrintWriter out=res.getWriter();
            out.println("");
            out.println("文件上传结果");
            out.println("");
            out.println("文件上传结果");
            out.println("ID为"+formfields.get("FileID")+"的文件"+filename+"已经上传!"+"文件长度:"+fileSize+"字节");
            out.println("");
            out.println("");
    }
    private static int byteIndexOf(byte[] b,String s,int start)
    {
        return byteIndexOf(b,s.getBytes(),start);
    }
    private static int byteIndexOf(byte[] b,byte[] s,int start)
    {
        int i;
        if(s.length==0)
        {
            return 0;
        }
        int max=b.length-s.length;
        if(maxmax)
            return -1;
        if(start

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • C++ MultiMaps 成员 swap():交换两个multimaps
  • 交换机和hub总是过一段时间掉了,品牌不一,总有数据在发送经hub到交换
  • C++ Lists(链表) 成员 swap():交换两个list
  • 从交换机分出来的机器的ip地址问题(高分求解)
  • C++ Maps 成员 swap():交换两个map
  • Linux的交换分区与/tmp目录有关系么?
  • C++ Vectors 成员 swap():交换两个Vector
  • 交换机上怎么防御ddos攻击和机器狗病毒啊
  • C++ Double Ended Queues(双向队列) 成员 swap():和另一个双向队列交换元素
  • 电话软交换机 FreeSWITCH
  • C++ Strings(字符串) 成员 swap():交换两个字符串的内容
  • 我说的对吗?夜猫子和想知道交换分区的人进
  • Python中变量交换的例子
  • 分布式 Checksum 交换中心 DCC
  • 请问这是什么型号的交换机??
  • linux交换分区怎么设置
  • 在交换机上实现SNMP
  • 交换芯片里面的vlan table 以及entries
  • 普通的ARM板子能实现简单的交换机的功能么
  • 讨论:内核,驱动,用户空间大批量数据交换的最佳方式
  • 编程实现对交换机的远程配置


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3