论坛首页 入门讨论版

java中数据上传

浏览 1430 次
该帖已经被评为新手帖
作者 正文
最后更新时间:2006-10-19 关键字: java
public void doUpload(HttpServletRequest req)throws ServletException, IOException
  {
    byte[] line=new byte[128];
    ServletInputStream in=req.getInputStream();//获得上传文件
    int i=in.readLine(line,0,128);
    /*
Reads the input stream, one line at a time. Starting at an offset,
reads bytes into an array, until it reads a certain number of bytes or reaches a newline character,
which it reads into the array as well. This method returns -1 if it reaches the end of the input
stream before reading the maximum number of bytes.
     return :integer specifying the actual number of bytes read,
    */

    if(i<3)
      return;

    int boundaryLength=i-2;
    String boundary=new String(line,0,boundaryLength);
    fields=new Hashtable();
    while(i!=-1)
    {
      String newLine=new String(line,0,i);
      if(newLine.startsWith("Content-Dispostion:form-data;name=\""));
      {
        if(newLine.indexOf("filename=\"")!=-1)//Returns the index within this string of the first occurrence of the specified substring.

        {
          setFilename(new String(line,0,i-2));//调用方法取出文件名
          if(filename==null)
            return;
          i=in.readLine(line,0,128);
          setContentType(new String(line,0,i-2));//调用方法
          i=in.readLine(line,0,128);

          i=in.readLine(line,0,128);
          newLine=new String(line,0,i);

          PrintWriter pw=new PrintWriter(new BufferedWriter(
      new FileWriter((savePath==null?  " ": savePath)  +filename)));

        //文件的最后一行包含换行符,因此必须检查当前行是否是最后一行
        while(i!=-1 && !newLine.startsWith(boundary))
        {
          i=in.readLine(line,0,128);
          if((i==boundaryLength+2 || i==boundaryLength+4)&&
            (new String(line,0,i).startsWith(boundary)))
           pw.print(newLine.substring(0,newLine.length()-2));
           else
             pw.print(newLine);
          newLine=new String(line,0,i);
        }
        pw.close();
        }
        else
        {
            int pos=newLine.indexOf("name=\"");
            String fieldName=newLine.substring(pos+6,newLine.length()-3);
            i=in.readLine(line,0,128);
            i=in.readLine(line,0,128);
            newLine=new String(line,0,i);
            StringBuffer fieldValue=new StringBuffer(128);
            while(i!=-1&&!newLine.startsWith(boundary))
            {
              i=in.readLine(line,0,128);
              if((i==boundaryLength+2 || i==boundaryLength+4)&&
                 (new String(line,0,i).startsWith(boundary)))
                fieldValue.append(newLine.substring(0,newLine.length()-2));
              else
                fieldValue.append(newLine);
              newLine=new String(line,0,i);
            }
            fields.put(fieldName,fieldValue.toString());
        }
      }
      i=in.readLine(line,0,128);

    }

  }用这种方法获得的数据,再打开的时候总是显示文件已被损坏,不能正常的打开是什么原因呀?
   
最后更新时间:2006-10-19
这个方法不错呀,应该不会有问题的
   
0 请登录后投票
最后更新时间:2006-10-19
这个不是处理二进制文件上传的程序.
   
0 请登录后投票
最后更新时间:2006-10-19
这都什么年代了,还自己写上传程序?
随便找一个现成的去用就行了。
真要有兴趣自己写,就慢慢调试吧,总会成功的。不需要考虑换行符的。
   
0 请登录后投票
最后更新时间:2006-10-20
RFC一句一句的对吧....有兴趣的话
   
0 请登录后投票
论坛首页 入门讨论版

跳转论坛:
JavaEye推荐