java kindeditor 图片上传返回json的问题

if (imgFile != null) {
File savefile = new File(new File(savePath), imgN);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
try {
FileUtils.copyFile(imgFile, savefile);
map.put("error", 0);
map.put("url", savefile);
JSONObject json = JSONObject.fromObject(map);
response.getOutputStream().print(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
FileUtils.copyFile(imgFile, savefile);
map.put("url", savefile);
map.put("error", 0);
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
response.getOutputStream().print(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
map.put("error", 1);
map.put("message", "上传失败");
JSONObject json = JSONObject.fromObject(map);
try {
response.getOutputStream().print(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
最新回答
浅色夏沫

2024-10-24 08:02:44

对象转换成json对象时出现死循环,你可在把map转换时加一个配置,如下
JsonConfig jc=new JsonConfig();
jc.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
//如果用到hibeinate的话可以加以下这句,如果map里面有不想输出的属性,把属性名也加到下面,可提高效率,以下这句可选,意思是过滤掉不需要转换成json对象的属性
jc.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
JSONObject json = JSONObject.fromObject(map,jc);
然后加JSONObject json = JSONObject.fromObject(map,jc);这句啊,单纯用SONObject json = JSONObject.fromObject(map);是直接把map对象的所有属性都转换到json对象中,加个JSONConfig是为了能可选可控的把map对象转换成json对象,
savefile是一个File对象,你map.put("url", savefile);改成map.put("url", savefile.getPath());反正不要把一个File对象put到map里就OK了
无人懂我

2024-10-24 08:07:31

你的问题是什么?或者说你遇到了什么问题?你列一段代码我不知道你啥意思啊