java中上传完文件删除不了的问题
我上传文件,把流都关闭了,然后还是删除不了,我贴下代码,高手看看是什么问题:if(ServletFileUpload.isMultipartContent(request...
我上传文件,把流都关闭了,然后还是删除不了,我贴下代码,高手看看是什么问题:
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(file1);
factory.setSizeThreshold(1024000);
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setSizeMax(Long.parseLong(filesize));
List items = null;
items = sfu.parseRequest(request);
Iterator it = items == null ? null : items.iterator();
while (it != null && it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()) {
int num = 0;
String filename = item.getName().substring(
item.getName().lastIndexOf("\\") + 1);
String tempext = filename.substring(filename
.lastIndexOf(".") + 1);
for (int i = 0; i < fileext.length; i++) {
if (fileext[i].equals(tempext)) {
num = 1;
}
}
if (num == 1) {
BufferedInputStream in = new BufferedInputStream(item.getInputStream());
String newFilename = filename;
if (os.equals("linux")) {
newFilename = new String(filename.getBytes(),"UTF-8");
}
File file = new File(path + file.separator+ newFilename);
if (!file.exists()) {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream out = new BufferedOutputStream(fos);
Streams.copy(in, out, true); result = "上传成功";
fos.close();
out.close();
} else {
result = "文件已存在";
}
in.close();
} else {
result = "不允许的文件类型";
}
}
}
} 展开
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(file1);
factory.setSizeThreshold(1024000);
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setSizeMax(Long.parseLong(filesize));
List items = null;
items = sfu.parseRequest(request);
Iterator it = items == null ? null : items.iterator();
while (it != null && it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()) {
int num = 0;
String filename = item.getName().substring(
item.getName().lastIndexOf("\\") + 1);
String tempext = filename.substring(filename
.lastIndexOf(".") + 1);
for (int i = 0; i < fileext.length; i++) {
if (fileext[i].equals(tempext)) {
num = 1;
}
}
if (num == 1) {
BufferedInputStream in = new BufferedInputStream(item.getInputStream());
String newFilename = filename;
if (os.equals("linux")) {
newFilename = new String(filename.getBytes(),"UTF-8");
}
File file = new File(path + file.separator+ newFilename);
if (!file.exists()) {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream out = new BufferedOutputStream(fos);
Streams.copy(in, out, true); result = "上传成功";
fos.close();
out.close();
} else {
result = "文件已存在";
}
in.close();
} else {
result = "不允许的文件类型";
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询