File file = new File("out.txt");
你的这句有问题,最好传个变量进去,标明类型,否则都是txt文件。

解决方案 »

  1.   

    把这个out.txt改成out.exe或者out.gif等等,是什么类型就改成什么后缀。
      

  2.   

    对不起:程序发错啦!
    package sm.net.upload;import java.util.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.ServletInputStream;public class upload {
      String me = "sm.net.upload.upload";
      int filecount = 0; //总共上传文件数量
      int currentNo = 0; //当前上传文件编号
      String division = ""; //分割字符 例如:----------7d32bf24409de  File file;
      FileOutputStream fos;
      ServletInputStream svins;  byte[] readByte = new byte[4096]; //输入流缓冲区
      int readCount; //readCount 记录从输入流中实际读取的字符数
      String line; //行
      String name,filename, targetpath; //上传名称,文件名称,目标路径,
      int key, len, index; //返回值,字符长度,index位置
      long cx = 0; //循环器  Hashtable ht = null;//上传的其他非文件值//----------------------------------------------------------------------------------
      public upload() {
        targetpath = "";
        readCount = 0;
        ht = new Hashtable();
      }//---------------------doUpload------------------------------------
    public void doUpload(HttpServletRequest req) throws ServletException,IOException
    {
      String fileinfo,No;//文件信息,编号
      boolean isFile;
      show("开始上传");
      try{
        svins = req.getInputStream();
          }catch(Exception e){
              show("初始化servletInputStream失败");
              svins.close();  return;           }
      //读取第一行
      readCount = svins.readLine(readByte, 0, readByte.length);
      line = new String(readByte);line = line.trim();
      //得到分割符
      division = line;
      show("分割符 division='"+division+"'");
      //开始循环读取数据
     while(readCount != -1){//while1
        //一个节点的分析与写入
       if (line.startsWith(division) ){//if1
         cx =cx +1;No = String.valueOf(cx);
         show("----发现节点分割符---"+No);
         //下读一行
         readCount = svins.readLine(readByte, 0, readByte.length);
         line = new String(readByte);line = line.trim();
         //确定是否为文件
         if (line.startsWith("content-")&&(line.indexOf("name=")!=-1)){//if2
           show("开始分析文件头"+No);
           if (line.indexOf("filename=")!=-1){//if3
             //是文件 得到文件名称
              isFile = true;
              index = line.lastIndexOf("\\");
              filename = line.substring(index+1);
              filename = filename.substring(0,filename.length()-1);
              if (filename.indexOf('"') !=-1){
                   index = filename.indexOf('"');
                   filename = filename.substring(0,index);
                  }
              show("filename:"+filename+No);
              //下读二行-------------
              readCount = svins.readLine(readByte, 0, readByte.length);
              readCount = svins.readLine(readByte, 0, readByte.length);
              //写文件---------------
              try{
                file = new File(filename);
                fos = new FileOutputStream(file);
                while ( (readCount != -1)) {
                  readCount = svins.readLine(readByte, 0, readByte.length);
                  line = new String(readByte);
                  line = line.trim();
                  if (line.startsWith(division))
                    break;
                  fos.write(readByte, 0, readCount);
                } //while
                //保存并关闭
                fos.flush();
                fos.close();
                file = null;
            }//try
              catch(Exception e){show("保存文件"+filename+"失败");return;}
             // continue;
              }else//if3------------------------------------------------------
              {//不是文件 保存到hashtable
                show("不是文件 保存到hashtable");
                //得到name值
                index = line.indexOf("name=");
                name = line.substring(index+5);
                name = name.substring(1,name.length()-1);
                if (name.indexOf('"') !=-1){
                   index = name.indexOf('"');
                   name = name.substring(0,index);
                   }
                show("name="+name+"---");
                //下读一行
                readCount = svins.readLine(readByte, 0, readByte.length);
                while((readCount != -1)){
                  readCount = svins.readLine(readByte, 0, readByte.length);
                  line = new String(readByte);line = line.trim();
                  if (line.startsWith(division)) break;
                }//while
              }//if3
           }//if2
        }//if1
        show("==================================");
     //   readCount = svins.readLine(readByte, 0, 1024);
     //   line = new String(readByte);line = line.trim();
       }//while1
       svins.close();
    }//doUpload2
    //-----------------目标路径----------------------------------
    public void setTargetPath(String path){
      this.targetpath=path;
    }
    public String getTargetPath(String path){
      return this.targetpath;
    }//------------------------close-----------------------------------
    private void close(){
        try{
          if (svins != null) svins.close();
          if (fos != null) fos.close();
        }catch(IOException ioe){}
        catch(Exception e){show("close()失败 ");return;}
    }
    ////////////////////////////show//////////////////////////////////////////////////
    private void show(Object Message){
       String err = String.valueOf(Message);
       System.out.println(this.me+"->show:     "+err);
    }
    }