@Override
public boolean deleteProductById(HttpServletRequest request,String ids) {
// 定義一個標記來表示是否刪除成功
boolean deleteFlag = false;
//刪除的時候需要刪除數據 並且把服務器的圖片也刪除
//得到上傳文件夾的路徑
// 1 ==>split(",") 1
// 1,2,3 ==> split(",") 數組
String file = request.getServletContext().getRealPath("/WEB-INF/upload");//upload是idea設置的虛擬路徑
if(ids !=null && ids !=""){
//進行拆分
String [] str = ids.split(",");
//對數組進行非空驗證
if(str !=null && str.length>0){
for (String s :str){
// 根據id查詢出對象
Product product = selectProductById(Integer.parseInt(s));
if(product ==null){
throw new RuntimeException("刪除的條件不匹配");
}else{
//獲取到文件名
String fileName = product.getPic();
if(fileName ==null || fileName==""){
throw new RuntimeException("此文件不存在");
}else{
//刪除文件
File newFile = new File(file,fileName);
boolean flag = newFile.delete();
// 刪除文件成功之後,再刪除數據
if(flag){
//調用dao層的刪除數據庫的數據
int num = productDao.deleteProductById(s);
if(num>0){
deleteFlag = true;
}
}
}
}
}
}
}
return deleteFlag;
}
另外一種寫法
// ----------------
public void deleteAllAndId(HttpServletRequest request, HttpServletResponse response) {
//刪除商品:
// 注意問題:既要刪除數據庫的數據也要刪除服務器的圖片
// 刪除服務器圖片操作:根據傳遞過來的選中的pid查詢出查出要刪除的對象集合
//將這個對象集合類的每個對象的圖片的名稱全部存到一個集合類
//再獲取服務器的路徑下全部指定文件路徑下全部文件名
// 通過比對兩個圖片名字時候相同,來判斷是否刪除
ServletContext servletContext = this.getServletContext();
String tmcatname = "WEB-INF/commodityImg";
try {
String ids = request.getParameter("ids");
//dao層方法
List<Product> productList = productService.selectProduct(ids);
List<String> fileName = new ArrayList<String>();
for (Product product : productList) {
fileName.add(product.getPic());
}
deleteFile(servletContext, fileName, tmcatname);
System.out.println(ids);
// System.out.println(pic.toString());
//dao方法
Integer integer = productService.deleteAll(ids);
PrintWriter writer = response.getWriter();
boolean flag = false;
if (integer > 0 && integer != null) {
flag = true;
writer.print(flag);
} else {
writer.print(flag);
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*刪除服務器的圖片文件*/
public void deleteFile(ServletContext servletContext, List<String> fileName, String filepath) {
String path = servletContext.getRealPath(filepath);//獲取服務端路徑
File file = new File(path);
String[] listFiles = file.list();//獲取服務器全部指定文件路徑下全部文件名
if (fileName == null) {//查詢的數據庫的圖片文件名集合時候爲空
return;
} else if (fileName != null && fileName.size() > 0) {
if (listFiles != null && listFiles.length > 0) {
//在確保數據庫查詢有數據圖片和服務器有有圖片文件的情況下
String filePath = null;
for (String str : fileName) {
filePath = path + "\\" + str;
for (int i = 0; i < listFiles.length; i++) {
if (str.equals(listFiles[i])) {//刪除和數據圖片的名相同的圖片
File file2 = new File(filePath);
if (file2.exists() && file2.isFile()) {//一直刪除完爲止
file2.delete();
}
}
}
}
}
}
}
查詢多個數據數據的dao
@Override
public List<Product> selectProduct(String cid) {//根據id查詢商品,可以根據多個cid進行查詢,多個或一個符合條件的商品,
// 這裏即可用作查詢單條記錄也可查詢多個圖片名用來刪除服務器的文件
String sql = "select * from product where pid in("+cid+")";
try {
List<Product> productList = DbUtils.qr.query(sql, new BeanListHandler<Product>(Product.class));
return productList;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}