有文件形如:
20050101 AAA 0.1
20050101 BBB 0.2
20050101 CCC 0.3
.....
20050101 ZZZ 0.26jsp页面如下:
<form name="fm" method="post" ENCTYPE="multipart/form-data">
<input type=file name="uploadFile">
</form>上传程序如下:
//得到FormFile文件
FormUpload tform = (FormUpload ) form;
FormFile myFile = tform.getUploadFile(); String varAttachment = myFile.getFileName(); path = "c://"+varAttachment;
InputStream is = null;
FileOutputStream fo = null; try {
fo = new FileOutputStream(path);
byte[] buff = new byte[1024];
is = myFile.getInputStream();
int longth = -1;
while ( (longth = is.read(buff)) > 0) {
fo.write(buff);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fo.close();
is.close();
}
catch (Exception e) {}
}但是上传以后得到的文件是:
20050101 AAA 0.1
20050101 BBB 0.2
20050101 CCC 0.3
.....
20050101 ZZZ 0.2620050101 AAA 0.1
20050101 AAA 0.1
20050101 BBB 0.2
20050101 CCC 0.3
......
20050101 ZZZ 0.26文件中字符之间的间隔符是制表符/t,为什么会写了两次呢?请大家帮我分析一下可能出错的地方,谢谢了
20050101 AAA 0.1
20050101 BBB 0.2
20050101 CCC 0.3
.....
20050101 ZZZ 0.26jsp页面如下:
<form name="fm" method="post" ENCTYPE="multipart/form-data">
<input type=file name="uploadFile">
</form>上传程序如下:
//得到FormFile文件
FormUpload tform = (FormUpload ) form;
FormFile myFile = tform.getUploadFile(); String varAttachment = myFile.getFileName(); path = "c://"+varAttachment;
InputStream is = null;
FileOutputStream fo = null; try {
fo = new FileOutputStream(path);
byte[] buff = new byte[1024];
is = myFile.getInputStream();
int longth = -1;
while ( (longth = is.read(buff)) > 0) {
fo.write(buff);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fo.close();
is.close();
}
catch (Exception e) {}
}但是上传以后得到的文件是:
20050101 AAA 0.1
20050101 BBB 0.2
20050101 CCC 0.3
.....
20050101 ZZZ 0.2620050101 AAA 0.1
20050101 AAA 0.1
20050101 BBB 0.2
20050101 CCC 0.3
......
20050101 ZZZ 0.26文件中字符之间的间隔符是制表符/t,为什么会写了两次呢?请大家帮我分析一下可能出错的地方,谢谢了
FileOutputStream out = new FileOutputStream(theFile);
out.write(thisForm.getTheFile().getFileData());
out.close();
fo = new FileOutputStream(path);
改成fo = new FileOutputStream(path,false);
是不是你upload按了两次?
把代码改成这样试试
while ( (longth = is.read(buff))!=-1) {
fo.write(buff,0,longth);
}