FileItem.write()方法,上传的文件,怎么删除呢?
# 具体遇到的问题
FileItem.write()方法,上传的文件,怎么删除呢?后台调用更新sql语句时,IDEA报错: Destination 已存在。
我想在更新操作时,删除原来FileItem.write()方法写入的文件,然后使用FileItem.write()方法将新上传的图片重新存到文件夹
# 报错信息的截图
org.apache.commons.io.FileExistsException: Destination 'D:\JavaSrc\book_manage_system-mybatis\out\artifacts\book_manage_system_mybatis_Web_exploded\resource\model\img\深度学习java编程.png' already exists
at org.apache.commons.io.FileUtils.moveFile(FileUtils.java:3001)
at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:405)
at com.imooc.book_manage.controller.BookServletController.create(BookServletController.java:104)
at com.imooc.book_manage.controller.BookServletController.doPost(BookServletController.java:37)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.imooc.book_manage.utils.CodeFilter.doFilter(CodeFilter.java:34)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
# 相关课程内容截图
# 尝试过的解决思路和结果
//这段代码无法删除path路径的文件
//path路径:'D:\JavaSrc\book_manage_system-mybatis\out\artifacts\book_manage_system_mybatis_Web_exploded\resource\model\img\深度学习java编程.png
try {
java.io.File myDelFile = new java.io.File(selectBook.getImgPath());
myDelFile.delete();
} catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
private void create(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload formData = new ServletFileUpload(factory);
Book book = new Book();
try {
List<FileItem> fiList = formData.parseRequest(request);
for(FileItem fi : fiList){
if(fi.isFormField()){
switch(fi.getFieldName()){
case "name":
book.setName(fi.getString("UTF-8"));
break;
case "categoryName":
String categoryId = fi.getString();
// System.out.println("categoryName"+categoryName);
// Category category = categoryService.selectByName(categoryName);
book.setCategoryId(Long.parseLong(categoryId));
break;
case "level":
book.setLevel(Integer.parseInt(fi.getString()));
break;
case "price":
book.setPrice(Integer.parseInt(fi.getString()));
break;
default:
break;
}
}else{
Book selectBook = bookService.selectByName(book.getName());
System.out.println("路径测试-->"+ selectBook);
if(selectBook == null){
// 当isFormField()方法返回结果为:false时,当前fileItem对象是-上传文件
String path = request.getServletContext().getRealPath("/resource/model/img");
// String name = UUID.randomUUID().toString();
String suffix = fi.getName().substring(fi.getName().lastIndexOf("."));
// StringBuffer strB = new StringBuffer(path);
// String addPath = String.valueOf(strB.append(fi.getName()));
fi.write(new File(path,book.getName()+suffix));
book.setImgPath(path+book.getName()+suffix);
System.out.println("图片路径-->" + path);
}else{
try {
java.io.File myDelFile = new java.io.File(selectBook.getImgPath());
myDelFile.delete();
} catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
String path = request.getServletContext().getRealPath("/resource/model/img");
String suffix = fi.getName().substring(fi.getName().lastIndexOf("."));
fi.write(new File(path,book.getName()+suffix));
book.setImgPath(path+book.getName()+suffix);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
//判断该图书名称是否已经存在,
// 存在则只更新表单提交信息和更新时间,
// 不存在,则将book(createTime)属性补齐后写入数据库
Book book1 = bookService.selectByName(book.getName());
if(book1 == null){
book.setCreateTime(new Date());
bookService.insert(book);
}else{
book.setUpdateTime(new Date());
bookService.update(book);
}
bookService.insert(book);
request.getRequestDispatcher("/book/query").forward(request,response);
}
正在回答 回答被采纳积分+1
同学你好,同学不需要删除文件。同学在上传文件时可以使用UUID生成随机的文件名。UUID生成的随机数几乎不会重复。
参考代码如下:
public static void main(String[] args) { //原始文件名 String originFileName = "test.jpg"; //获得后缀名 String suffix = originFileName.substring(originFileName.lastIndexOf(".")); String fileName = UUID.randomUUID().toString(); //新文件名 String newFileName = fileName + suffix; System.out.println("新文件名 "+newFileName); }
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星