我用javaBean 自己写了一个文件上传 自己解析文件输入流 但是每次上传的文件在结尾都比源文件多出一个换行符
代码如下:
package up;/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author elliott
 */import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.io.File;
import java.util.Vector;
public class MyUpload {
    private String fileName;
    private String fileExtName;
    private String fileDir;
    private String argu;/*上传的表单名字*/
    long size;
    boolean coverAllow;/*是否允许同名覆盖*/
    Vector errors;
public MyUpload(){
    fileName=null;
    fileExtName=null;
    fileDir=null;
    argu=null;
    size=0;
    coverAllow=false;
    errors=new Vector();
}
public void setFileDir(String dir){
   this.fileDir=dir;
}
public void setArgu(String argu){
    this.argu=argu;
}
public void setCoverAllow(boolean cva){
    this.coverAllow=cva;
}
public String getFileDir(){
return fileDir;
}
public String getArgu(){
 return argu;
}
public Vector getErrors(){
    return errors;
}
public boolean upload(InputStream servInS,String _argu,boolean _coverAllow){                  /*上传文件*/
    this.setArgu(_argu);
    this.setCoverAllow(_coverAllow);
    boolean legal=true;
    File tarFile=null;
    File temFile=new File(fileDir+"/tem");                           /*建立临时缓冲文件*/
    if(!temFile.getParentFile().exists()){
     temFile.getParentFile().mkdirs();
    }
        try {
            byte b[]=new byte[1024];
            int n=-1;
            FileOutputStream temOutS = new FileOutputStream(temFile);
            while((n=servInS.read(b,0,1024))!=-1){
                temOutS.write(b,0, n);
            }
            temOutS.close();
        } catch (FileNotFoundException ex) {
            legal=false;
           errors.addElement(ex.getMessage());
        }
        catch(IOException e){
          errors.addElement("试图建立临时数据文件失败!"+e.getMessage());
          return false;
        }
        try {
            long start=0,end=0;
            RandomAccessFile in = new RandomAccessFile(temFile,"rw");
            in.seek(0);
            String boundary=in.readLine();
            in.seek(0);
            boolean findSta=false;
            while(true){
                String str=null;
                while((str=in.readLine())!=null&&str.indexOf(boundary)==-1){
                    end=in.getFilePointer();
                }
               if(findSta==true){
               break;
               }
                str=in.readLine();
                if(str==null){
                 errors.addElement("没有"+argu+"这个参数!");
                return false;
                }
                String s="name=\""+argu+"\"";
                if((!findSta)&&(str.indexOf(s))!=-1){
                    int loc=str.lastIndexOf("\\");
                     fileName=str.substring(loc+1,str.length()-1);
                    in.readLine();
                    in.readLine();
                    start=in.getFilePointer();
                    findSta=true;
                }
            }
           tarFile=new File(fileDir+"/"+fileName);
            if(tarFile.exists()&&!coverAllow){
                errors.addElement("本系统不允许同名覆盖!");
                 return false;
           }
           FileOutputStream tar= new FileOutputStream(tarFile);
           byte b[]=new byte[2];
           in.seek(start);
           while(start<end){
               in.read(b,0,1);
               tar.write(b,0,1);
               start++;             /* int n=-1;
               if(start+1024<end){
                 n=in.read(b,0,1024);
                  tar.write(b,0,n);
                   start+=n;
               }
               else{
                 n=-1;
                 n=in.read(b,0,(int) (end - start));
                 tar.write(b,0,n);
                 start+=n;
               }*/
           }           in.close();
           tar.close();        } catch (FileNotFoundException ex) {
           errors.addElement(ex.toString());
        }
        catch(IOException e){
        errors.addElement(e.toString());
        }    return true;
}
}