本帖最后由 lokichao 于 2009-08-08 16:54:09 编辑

解决方案 »

  1.   

    request.getInputStream(); 
    你真有趣,在jsp里面还想处理输入流?
      

  2.   

    upfile.jsp<%@ page contentType="text/html;charset=gb2312" %>
    <%@ page import="java.io.*" %>
    <html>
    <body>
       <%     String tempfilename=(String)session.getId();
              File f1=new File("D:\\JAVA\\Tomcat\\webapps\\fileup",tempfilename);
              int n;
              try
              {
                  InputStream in=request.getInputStream();
                  BufferedInputStream my_in=new BufferedInputStream(in);
                  FileOutputStream fout=new FileOutputStream(f1);
                  BufferedOutputStream my_out=new BufferedOutputStream(fout);
                  byte[] b=new byte[10000];
                  while((n=my_in.read(b))!=-1)
                  {
                       my_out.write(b,0,n);
                  }
                  my_out.flush();
                  my_out.close();
                  fout.close();
                  my_in.close();
                  in.close();
                  out.print("文件上传成功!<br>");
              }
              catch(IOException e)
              {
                  out.print("文件上传失败!");
              }
              
              try
              {   
                  RandomAccessFile random1=new RandomAccessFile(f1,"r");
                  random1.readLine();
                  String filename=random1.readLine();
                  byte[] b=filename.getBytes("ISO-8859-1");
                  filename=new String(b);
                  int pointer=filename.lastIndexOf('\\');
                  filename=filename.substring(pointer+1,filename.length()-1);
                  File f2=new File("J:\\Tomcat 5.5\\webapps\\ROOT\\test",filename);
                  RandomAccessFile random2=new RandomAccessFile(f2,"rw");
                  random1.seek(0);
                  for(int i=1; i<=4; i++)
                  {
                       String tempstr=random1.readLine();
                  }
                  long startPoint=random1.getFilePointer();
                  random1.seek(random1.length());
                  long =random1.getFilePointer();
                  int j=0;
                  long endPoint=0;
                  while((>=0)&&(j<=5))
                  {
                       --;
                       random1.seek();
                       n=random1.readByte();
                       if(n=='\n')
                       {
                             j++;
                             endPoint=random1.getFilePointer();
                       }
                  }
                  long length=endPoint-startPoint+1;
                  int order=(int)(length/10000);
                  int left=(int)(length%10000);
                  byte[] c=new byte[10000];
                  random1.seek(startPoint);
                  for(int i=0; i<order; i++)
                  {
                        random1.read(c);
                        random2.write(c);
                  }
                  random1.read(c,0,left);
                  random2.write(c,0,left);
                  random1.close();
                  random2.close();
                  f1.delete();
                  out.print("文件更新成功!");
              }
              catch(Exception e)
              {
                  out.print("文件更新失败!");
              }
       %>
    </body>
    </html>
      

  3.   

    uploadBean.java/*
     * Created on 2009-8-8
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package MyBean;/**
     * @author innolux_08
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */import  java.io.*;
    import javax.servlet.http.HttpServletRequest;
    public class uploadBean {
     public void doUpload(HttpServletRequest request) throws 
         IOException { 
              //String filename=(String)session.getId();
              File tempf=new File("D:\\JAVA\\Tomcat\\webapps\\fileup","2");
              int n;
              try
              {
                  InputStream in=request.getInputStream();
                  BufferedInputStream my_in=new BufferedInputStream(in);
                  FileOutputStream fout=new FileOutputStream(tempf);
                  BufferedOutputStream my_out=new BufferedOutputStream(fout);
                  byte[] b=new byte[10000];
                  while((n=my_in.read(b))!=-1)
                  {
                       my_out.write(b,0,n);
                  }
                  my_out.flush();
                  my_out.close();
                  fout.close();
                  my_in.close();
                  in.close();
                  //out.print("文件上传成功!<br>");
              }
              catch(IOException e)
              {
                  //out.print("文件上传失败!");
              }
     }}
      

  4.   

    http://download.csdn.net/source/1532774
    上面有完整的例子
      

  5.   

    楼主你这个代码也叫上传文件啊
    网页中上传文件的时候HttpInputStream 里有HTTP请求头的信息,和文件的内容
    并不只有单一的文件内容,所以你要在服务器段对HttpInputStream  进行解析才能
    获取到文件的内容
    楼主你就直接用那个cos 上传文件的组件包就可以了
    顺便可以看看里面的源代码