数据库连接没问题,文件流再写入数据库的时候报了异常,请大家帮忙看下
request.setAttribute("upFile",upFile);
String tempFileName=(String)session.getId();
InputStream in=request.getInputStream();
String saveFileName=loginName;
//上传文件保存的物理路径
//System.out.println("执行到这里的ytryyt");
String driverPath = request.getRealPath("image");
//System.out.println(driverPath);
uploadFileBll upbll=new uploadFileBll();
boolean flag=upbll.uploadFileMethod(driverPath,tempFileName,saveFileName,in);
System.out.println(flag);
System.out.println("执行到这里的");
if(flag){
examineeBll exambll=new examineeBll();
int n=exambll.setExamineePic(
loginName,upbll.getSavedFileName());
if(n==1){
backMessage="文件上传成功!";
flag一直返回 false
public boolean uploadFileMethod(String driverPath,String tempFileName,String saveFileName,InputStream fileSource){
//backMessage="上传文件的扩展名不符合要求!";
try{
uploadFileDal upFile=new uploadFileDal( driverPath, tempFileName, saveFileName, fileSource);
//System.out.println(upFile);
flag=upFile.uploadFileMethod();
backMessage=upFile.getBackMessage();
//System.out.println(backMessage);
this.setUploadFileName(upFile.getUploadFileName());
this.setSavedFileName(upFile.getSaveFileName());
}
catch(Exception exp){
flag=false;
backMessage="上传文件失败!";
this.setUploadFileName("");
this.setSavedFileName("");
}
return flag;
public boolean uploadFileMethod(){
try{//上传文件保存到文件夹
File f1=new File(driverPath,tempFileName);
FileOutputStream fos=new FileOutputStream(f1);
byte b[]=new byte[10000];
int n;
while((n=fileSource.read(b))!=-1){
fos.write(b,0,n);
}
fos.close();
fileSource.close();
//读取临时文件夹中的第二行内容
RandomAccessFile random=new RandomAccessFile(f1,"r");
int second=1;
String secondLine=null;
while(second<=2)
{
secondLine=random.readLine();
second++;
}
//得到上传文件名
int position=secondLine.lastIndexOf('\\');
uploadFileName=secondLine.substring(position+1,secondLine.length()-1);
//转换编码,识别汉字文件名
byte cc[]=uploadFileName.getBytes("iso-8859-1");
uploadFileName=new String(cc);
//得到上传文件的扩展名
int extposition=uploadFileName.lastIndexOf('.');
String extName=uploadFileName.substring(extposition+1,uploadFileName.length());
//获取上传临时文件的第四行回车符的位置
random.seek(0);
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1 && forth<=4){
if(n=='\n'){
forthEndPosition=random.getFilePointer();
}
}
//删除文件
saveFileName=saveFileName.concat("."+extName);
File dir=new File(driverPath);
dir.mkdir();
File file[]=dir.listFiles();
for(int k=0;k<file.length;k++){
if(file[k].getName().equals(saveFileName)){
file[k].delete();
}
}
//按新文件名保存文件
File savingFile=new File(driverPath,saveFileName);
RandomAccessFile random2=new RandomAccessFile(savingFile,"rw");
//在临时文件夹中获得文件结束位置
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&& (j<=6)){
mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n'){
endPosition=random.getFilePointer();
j++;
}
}
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
while(startPoint<=endPosition-1){
n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();
random.close();
f1.delete();
flag=true;
backMessage="成功上传";
}
catch(Exception exp){
System.out.println("uploadFileMethod"+exp.toString());
backMessage=" "+exp;
flag=false;
}
return flag;
}
}