我的程序思想是这样的:用jspsmart上传一个文件到服务器指定目录,然后服务器将文件读入到数据库,最后程序自动将上传的文件删除。前面的都能够正常实现,就是最后删除的时候总是失败,请各位朋友看看。相关代码如下:
//get the name of the upload file
myFileName = myFile.getFileName();
//get the name the file without suffix
String suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
//get suffix of the file
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
//get the size of the file
int fileSize=myFile.getSize();
//store the path
//String aa=getServletContext().getRealPath("/")+"jsp\\";
String aa = request.getRealPath("/")+"temp\\";
String trace=aa+ myFileName;
System.out.println("trace = " + trace);
//store the file in server
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
//store the upload file in the databank
file = new java.io.File(trace);
fis = new java.io.FileInputStream(file); ......... if (file.exists()) {
boolean flag = file.delete();
if(!flag){
System.out.println("Deletion failed ");
}
}
程序运行后总是弹出Deletion failed,也就是删除失败,不知道为什么!
//get the name of the upload file
myFileName = myFile.getFileName();
//get the name the file without suffix
String suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
//get suffix of the file
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
//get the size of the file
int fileSize=myFile.getSize();
//store the path
//String aa=getServletContext().getRealPath("/")+"jsp\\";
String aa = request.getRealPath("/")+"temp\\";
String trace=aa+ myFileName;
System.out.println("trace = " + trace);
//store the file in server
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
//store the upload file in the databank
file = new java.io.File(trace);
fis = new java.io.FileInputStream(file); ......... if (file.exists()) {
boolean flag = file.delete();
if(!flag){
System.out.println("Deletion failed ");
}
}
程序运行后总是弹出Deletion failed,也就是删除失败,不知道为什么!
你这里不是打开了一个流吗,你不是要往一个地方写吗
写完之后fis.close();然后才能 file.delete();