在后台得到一个Excel文件的路径,然后用使用BufferedInputStream和BufferOutputStream将文件保存到服务器,然后读取文件中的数据并将它们保存到数据库中。但是问题出现了:每次都复制了,但是在判断BufferedInputStream的read方法的时候,read的结果都是-1.而且被复制的原文件中也变成了空的。实在是焦头烂额啊,哪位大侠指点一下。下面是代码:
private static final int BUFFER_SIZE = 16 * 1024 ;
    
    private File myFile;
    private String contentType;
    private String fileName;
    private String imageFileName;
    private String caption;
   
    public void setMyFileContentType(String contentType)  {
        this .contentType = contentType;
   } 
   
    public void setMyFileFileName(String fileName)  {
        this .fileName = fileName;
        
    
        
   } 
       
    public void setMyFile(File myFile)  {
        this .myFile = myFile;
   } 
   
    public String getImageFileName()  {
        return imageFileName;
   } 
   
    public String getCaption()  {
        return caption;
   }      public void setCaption(String caption)  {
        this .caption = caption;
   } 
   
    private static void copy(File src, File dst)  {
        try  {
           InputStream in = null ;
           OutputStream out = null ;
            try  {                
               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
               out = new BufferedOutputStream( new FileOutputStream(src), BUFFER_SIZE);
                byte [] buffer = new byte [BUFFER_SIZE];
                System.out.println("buffer.length:"+buffer.length);
                System.out.println("____________赋值文件:"+in.available()+"字节");                //这里每次都是-1
                while (in.read(buffer) > 0 )  {
                   out.write(buffer);
               }
 
//                将缓冲区中的数据全部写出 
//                out.flush();
            } finally  {
                if ( null != in)  {
                   in.close();
               } 
                 if ( null != out)  {
                   out.close();
               } 
           } 
        } catch (Exception e)  {
           e.printStackTrace();
       } 
   } 
      public String doExcel() {  
   String str = this.getRequest().getParameter("filepath");
   try {
fileName = URLDecoder.decode(URLDecoder.decode(str, "utf-8"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();

this.myFile = new File(fileName);
       imageFileName = new Date().getTime() + ".xls";
       File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/Excel" ) + " / " + imageFileName);
       copy(myFile, imageFile);
       setMessage("成功!");
       return "successMessage";
   } 

解决方案 »

  1.   

    对excel文件的内容这样应该是不行的!想读excel中的内容用poi吧!
      

  2.   

    in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
    out = new BufferedOutputStream( new FileOutputStream(src), BUFFER_SIZE); //这里有问题吧,源文件和目标文件是同一个,改成new FileOutputStream(dst)
      

  3.   

    这个只是将Excel文件保存到服务器上面的代码,在action中我还有读取并保存excel数据的方法,现在的问题就是无法完成保存Excel文件到服务器。没底read返回的结果都是-1,而且被读过的文件都变成了空的,啥也没了
      

  4.   


    LZ就没看我的回复?你的代码有问题,输入输出都是同一个文件,当你new FileOutputStream(src),你的源文件就被清空了,当然read返回的结果只能是-1了
      

  5.   

    我刚开始是new FileOutputStream(dst),后来改成src准备瞎猫碰死耗子呢,在发帖的时候没注意到,结果就这样了
      

  6.   

    我改回来了,但是在执行out = new BufferedOutputStream( new FileOutputStream(src), BUFFER_SIZE); 这句代码报了一个错误,说是找不到路径C:\Program Files\PlantixStudio1.6\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\statistics\Excel \ 1303268630406.xls (系统找不到指定的路径。)
    这是怎么回事?1303268630406.xls这个文件没有复制到位?
      

  7.   

    自己定义的 File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/Excel" ) + " / " + imageFileName);
      

  8.   

    File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/Excel" ) + " / " + imageFileName); //是不是路径有空格的问题
    改成getRealPath( "/Excel" ) + "/" + imageFileName试试看
      

  9.   

    首先你可以先确认一下是否存在路径
    C:\Program Files\PlantixStudio1.6\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\statistics\Excel