package com.mandi.servlet; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.mandi.common.RequestParam; import com.mandi.common.SessionMethod; @WebServlet(name="removefile",urlPatterns="/removefileservlet.sv") public class Removefileservlet extends HttpServlet { private static final long serialVersionUID = 3156691139583344145L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { JsonObject jo=new JsonObject(); Gson g=new Gson(); resp.setCharacterEncoding("UTF-8"); resp.setContentType("application/json; charset=utf-8"); String url=RequestParam.getSqlString(req, "url"); url=req.getServletContext().getRealPath(url); Path p=Paths.get(url); if(!Files.exists(p)) { jo.addProperty("code", 1); jo.addProperty("errmsg", "文件不存在!"); SessionMethod.writeresp(resp, g.toJson(jo)); return; } try{ Files.delete(p); jo.addProperty("code", 0); }catch (Exception e) { e.printStackTrace(); jo.addProperty("code", 2); jo.addProperty("errmsg", "文件不能删除!!"); } SessionMethod.writeresp(resp, g.toJson(jo)); return; } }