老师,我的图片删除失败

来源:5-1 删除分类修改关联商品的外键

litter_

2019-06-29 19:54:48

老师,我使用使用this.getServletContext().getRealPath(path);获取到的绝对路径是错的,它在中间的地方显示了两个.../shop/shop/upload/... (shop是我的工程名),导致我的照片无法被删除,我的图片也是存的绝对路径如下:D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp6\wtpwebapps\shop\upload

在开发工具中显示的绝对路径地址是如下:

D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp6\wtpwebapps\shop\shop\upload\1aeb0c74c90146e98de2d4c1b6cd7ee9.jpg

可以看到两者之间多了一个 /shop 导致我的照片删除失败,

还有为什么我的照片会存在eclipse的目录下,而在Tomcat下没有找到的?

写回答

1回答

好帮手慕阿满

2019-06-30

同学你好,问一下同学的this.getServletContext().getRealPath(path);中的path是什么呢?建议同学将删除图片这部分的代码贴一下。另外同学应该是将项目发布到eclipse目录下,而不是tomcat目录中,所以图片是在eclipse目录中。

祝:学习愉快~

0
hitter_
h /** * 后台商品管理,修改商品的方法 * @param request * @param response * @throws IOException */ private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException { //接受参数 Integer pid = Integer.parseInt(request.getParameter("pid")); //调用业务层处理数据 ProductService productService = new ProductServicelmpl(); //查询商品信息 Product product = productService.findOne(pid); String path = product.getPath(); if(path != null&& !"".equals(path)) { //获得文件的绝对路径 String realPath = this.getServletContext().getRealPath(path); System.out.println(realPath); File file = new File(realPath); if(file.exists()) { file.delete(); } } //删除产品信息 productService.delete(pid); //进行页面跳转 response.sendRedirect(request.getContextPath()+"/ProductServlet?method=findAll"); }
h019-06-30
共1条回复

0 学习 · 8016 问题

查看课程