hjg
2024-03-18 8d48c59d5d3bcc4148ef97dc6f98e3a8cf9ca436
提交 | 用户 | 时间
58d006 1 package com.mandi.servlet;
A 2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7
8 import javax.servlet.ServletException;
9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import com.google.gson.Gson;
15 import com.google.gson.JsonObject;
16 import com.mandi.common.RequestParam;
17 import com.mandi.common.SessionMethod;
18 @WebServlet(name="removefile",urlPatterns="/removefileservlet.sv")
19 public class Removefileservlet extends HttpServlet {
20     private static final long serialVersionUID = 3156691139583344145L;
21
22     @Override
23     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
24             throws ServletException, IOException {
25         this.doGet(req, resp);
26     }
27
28     @Override
29     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
30             throws ServletException, IOException {
31         JsonObject jo=new JsonObject();
32         Gson g=new Gson();
33         resp.setCharacterEncoding("UTF-8");
34         resp.setContentType("application/json; charset=utf-8");
35         String url=RequestParam.getSqlString(req, "url");
36         url=req.getServletContext().getRealPath(url);
37         Path p=Paths.get(url);
38         if(!Files.exists(p))
39         {
40             jo.addProperty("code", 1);
41             jo.addProperty("errmsg", "文件不存在!");
42             SessionMethod.writeresp(resp, g.toJson(jo));
43             return;
44         }
45         try{
46             Files.delete(p);
47             jo.addProperty("code", 0);
48         }catch (Exception e) {
49             e.printStackTrace();
50             jo.addProperty("code", 2);
51             jo.addProperty("errmsg", "文件不能删除!!");
52         }
53         SessionMethod.writeresp(resp, g.toJson(jo));
54         return;
55     }
56     
57 }