我是用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>
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测试题 跪求解答!!
- session.beginTransaction()时出现空指针异常,却没有报错
- 使用Dwr map中的Bean值无法获取
- getSession和openSession的区别
- mysql limit 不好使
- 菜鸟再次提问:关于J2EE
- struts2中select标签的问题
- ftpClient中的openServer()方法参数中的主机名不输入IP地址,输入域名支不支持
- JCE(Java 密码术扩展) JSSE(Java 安全套接字扩展) JAAS(Java 认证和授权服务)
- java连接sqlServer数据库出错
- JAVA测试题
- 求教自己编写的简单留言系统为什么会注册失败问题
if如果file存在 则
file.delete();
else没有就打印下
没有+savePath+shopPoster.getContext()+文件
貌似你的这些代码我没看到传个集合或者是数组什么的?
那怎么能保证批量删除呢?
起码可以弄个checkbox 传递给action的集合或者数组然后在批量删
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();
}
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()是空的,我存是用这属性存的路径
2.删除文件的时候调试一下,看你删除的时候文件路径是否存在