当前位置: 代码迷 >> J2EE >> 删除服务器上的图片,该如何处理
  详细解决方案

删除服务器上的图片,该如何处理

热度:37   发布时间:2016-04-22 01:44:35.0
删除服务器上的图片
我是用ss2h做的我上传图片到服务器上,地址上传到数据库中,这是成功的,可是我删除就不行了,服务器上的图片删不了,获取不到img的值,我都找了好久了,实在没办法,还得请大神们看看
public String deleteTest(){
testBiz.delTest(shopPoster.getId());
String savePath = ServletActionContext.getServletContext().getRealPath(
""); // 获取项目根路径
  savePath = savePath + "/upload/";
File file=new File(savePath+shopPoster.getContext());
file.delete();
resultMap="testActionshow.action";
return SUCCESS;
}
jsp上的 <script type="text/javascript">
  function del(id){
  location.href="shopEcsVoteOptiondeletePoster.action?shopPoster.id="+id
  }
  function ret(){
  location.href="addPoster.jsp";
  }
 </script>
  <body ><!-- 
  <form action="shopEcsVoteOptiondeletePosters.action" method="post" name="myForm">
  --><table align="center" border="1">
  <s:iterator value="#request['listShopPoster']" var="listShopPoster">
  <tr>
  <td>
  <img src="Poster/<s:property value="#listShopPoster.context"/>" height="50" width="50" />
  </td>
  <td>
  <input type="button" value="删除" onclick="del(<s:property value="#listShopPoster.id"/>)">
  </td>
  </tr>
</s:iterator>
<tr>
<td>
<input type="button" value="继续添加" onclick="ret()">
</td>
</tr>
  </table><!--
  </form>
  --></body>

------解决方案--------------------
探讨

那我删一条可以,批量就不可以,这是神马情况

------解决方案--------------------
给你传一个我用来删除图片的
Java code
@Override    public void init() throws ServletException {        try {            String tomcatPath = System.getProperty("user.dir");            String filePath =  tomcatPath.substring(0,tomcatPath.length()-4)+"/webapps/ios/images";            File file = new File(filePath);            if(file.exists()&&file.isDirectory()){                File [] files = file.listFiles();                for(int i=0;i<files.length;i++){                    if(files[i].isFile()){                        files[i].delete();                    }                }            }            System.out.println("<提示:清理图片缓存成功!>");        } catch (Exception e) {            System.out.println("<提示:清理图片缓存失败!>");            e.printStackTrace();        }        super.init();    }
  相关解决方案