使用Json过程中的一些问题

本文介绍了一个Java Web应用程序中用于创建文件夹的API实现方法。通过接收HTTP POST请求,该API允许用户指定文件夹名称及路径,并利用fileService进行文件夹的实际创建操作。若创建成功,则返回创建日期;若失败则返回错误信息。此外,还提供了一种备选方案以应对NoClassDefFoundError异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RequestMapping(value = "doAddFolder", method = RequestMethod.POST)
public void AddFolder(HttpServletRequest request,
HttpServletResponse response) {
String userName = (String) request.getSession().getAttribute("account");
String name = request.getParameter("name");
String path = request.getParameter("path");
// String date = testcaseService.createFolder(path, name);
String date = "";
String msg = "";
try {
date = fileService.addFile(userName, path, name);
} catch (Exception e) {
msg = "You are not allowed to create new file here.";
}
JSONObject jsonobj = new JSONObject();
jsonobj.put("user_runner", Constants.USER_ROLE_RUNNER);
jsonobj.put("msg", msg);
jsonobj.put("date", date);
this.sendJson(response, jsonobj);
}

public void sendJson(HttpServletResponse response, JSONObject jsonobj) {
try {
PrintWriter out = response.getWriter();
out.print(jsonobj);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

 

如果 报NoClassDefFoundError,也可使用:

public void getAvailableAgent(HttpServletRequest request,
HttpServletResponse response) {
String path = request.getParameter("path");
path = path+":pht";
// JSONObject jsonObj = new JSONObject();
// jsonObj.put("path", path);
Gson gson = new Gson();
String jsonObj = gson.toJson(path);
System.out.println("path:"+path);
this.sendJson(response, jsonObj);
}

public void sendJson(HttpServletResponse response, String jsonobj) {
try {
PrintWriter out = response.getWriter();
out.print(jsonobj);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

 

转载于:https://www.cnblogs.com/puhongtao/p/6676970.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值