我是用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>

解决方案 »

  1.   

      在img添加个name="shopPoster.context"也不行
      

  2.   


    if如果file存在 则
    file.delete();
    else没有就打印下
    没有+savePath+shopPoster.getContext()+文件
    貌似你的这些代码我没看到传个集合或者是数组什么的?
    那怎么能保证批量删除呢?
    起码可以弄个checkbox 传递给action的集合或者数组然后在批量删 
      

  3.   

    给你传一个我用来删除图片的@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();
    }
      

  4.   

    批量删的话,做个arraylist 把需要删的 add到里面 然后clear();试试
      

  5.   

    public String deletePosters(){
    shopManageService.deleteShopPosters(shopPosterIds);
    try {
    for(int i=0;i<shopPosterIds.length;i++){
    String savePath = ServletActionContext.getServletContext().getRealPath(""); // 获取项目根路径
            savePath = savePath + "/Poster/";
            savePath=savePath.replace("\\", "/");
            context=context.substring(context.lastIndexOf("/")+1);
    File file=new File(savePath+shopPoster.getContext());
    file.delete();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    resultMap="shopEcsVoteOptionlisPoster.action";
    return "index";
    }
    我这样写有错吗??shopPoster.getContext()是空的,我存是用这属性存的路径
      

  6.   

    1.上传的时候记得一定要flush一下确认流缓冲处理,并把流关闭掉,否则即便文件存在也是删除不掉的。
    2.删除文件的时候调试一下,看你删除的时候文件路径是否存在
      

  7.   

      是呀,location.href="shopEcsVoteOptiondeletePoster.action?shopPoster.id="+id+"&context="+img;你看我删除一条都是传值过来的,我就想不明白了,我的地址是存到context字段里,我用类点get这属性为什么是空??