String oldName=fi.getName(); String houzhui=oldName.substring(oldName.lastIndexOf('.')); String newName= UUID.randomUUID()+houzhui; String path0=application.getRealPath("img"); out.print(path0+"<br/>"); File path=new File(path0); if(!path.exists()) path.mkdirs(); fi.write(new File(path,newName)); student.setIdcardpic("img/"+newName);如何进行图片修改并删除之前的图片,如果图片未修改则保存之前的图片
时间: 2024-03-26 18:36:51 浏览: 11
这段代码是用来上传图片并将其保存在服务器的指定路径下的。如果你想要修改图片并删除之前的图片,可以按照以下步骤进行操作:
1. 首先获取之前保存的图片的路径,例如:String oldPicPath = "img/xxx.jpg";
2. 判断是否有新的图片要上传,如果有则将新图片保存到服务器,并获取新图片的路径,例如:String newPicPath = "img/yyy.jpg";
3. 如果新图片路径不为空,则说明有新图片上传,此时需要将旧图片删除,可以使用以下代码删除旧图片:File oldPicFile = new File(application.getRealPath(oldPicPath));oldPicFile.delete();
4. 否则,如果新图片路径为空,则说明没有新图片上传,此时需要将旧图片路径赋值给student.setIdcardpic,例如:student.setIdcardpic(oldPicPath);。
5. 最后,将新图片路径赋值给student.setIdcardpic,例如:student.setIdcardpic(newPicPath);。
注意:在删除旧图片之前,要确保新图片已经成功保存到服务器上。
相关问题
String oldName=fi.getName(); String houzhui=oldName.substring(oldName.lastIndexOf('.')); String newName= UUID.randomUUID()+houzhui; String path0=application.getRealPath("img"); out.print(path0+"<br/>"); File path=new File(path0); if(!path.exists()) path.mkdirs(); fi.write(new File(path,newName)); student.setIdcardpic("img/"+newName);将添加图片改成修改图片并删除之前的图片
好的,你可以将以下代码添加到你的代码中,以实现修改图片并删除之前的图片。
```java
String oldName = fi.getName();
String houzhui = oldName.substring(oldName.lastIndexOf('.'));
String newName = UUID.randomUUID() + houzhui;
String path0 = application.getRealPath("img");
out.print(path0+"<br/>");
File path = new File(path0);
if(!path.exists()) {
path.mkdirs();
}
String oldPic = student.getIdcardpic(); // 获取之前的图片路径
File oldFile = new File(application.getRealPath(oldPic)); // 根据路径获取文件对象
if (oldFile.exists()) { // 如果文件存在
oldFile.delete(); // 删除文件
}
fi.write(new File(path,newName));
student.setIdcardpic("img/" + newName);
```
这段代码会将新上传的图片保存到服务器中,并将旧的图片删除。请注意,这里假设 `student` 对象中有一个 `idcardpic` 属性,用来保存图片的路径。你需要根据实际情况修改代码。
oldname=os.path.join(path,i)
`oldname=os.path.join(path,i)`是一个将路径和文件名拼接起来的操作。其中,`path`是路径,`i`是文件名。这个操作可以用来生成一个完整的文件路径。
举个例子:
```python
import os
path = "/home/user/documents"
i = "file.txt"
oldname = os.path.join(path, i)
print(oldname) # 输出:/home/user/documents/file.txt
```
这个例子中,`path`是"/home/user/documents",`i`是"file.txt",通过`os.path.join()`函数将路径和文件名拼接起来,生成了完整的文件路径"/home/user/documents/file.txt"。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)