51,409
社区成员
发帖
与我相关
我的任务
分享 @RequestMapping(value="uploadfile",method = RequestMethod.POST, produces={"application/json;charset=UTF-8"})
@ResponseBody
public String file(HttpServletRequest req) throws ServletException,IOException
{
ResJson<String> res=new ResJson<String>();
String fileType = "png";
byte[] contentBytes = Base64.decodeBase64(Const.getPara(req, "file"));
int length = contentBytes.length;
ClassPathResource outFile = new ClassPathResource("/");
String p = outFile.getPath();
p = outFile.getFile().getParentFile().getParent();
String rePath = "/userfiles/";
String path = p + rePath;
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
String fileName = UUID.randomUUID().toString().replaceAll("-", "")
+ "." + fileType;
path = path + fileName;
rePath = rePath + fileName;
try {
FileCopyUtils.copy(contentBytes, new File(path));
} catch (IOException e) {
e.printStackTrace();
return JSON.toJSONString(res.getFailedJson("上传失败"));
}
return JSON.toJSONString(res.getSucJson("上传成功"));
} 



