欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
java:
@ResponseBody
@RequestMapping(method = RequestMethod.POST, value = “/uploadUserPic.html”)
public String saveUserPic(HttpServletRequest request,
@RequestParam(value = “userPic”, required = true) MultipartFile file)
throws Exception {
unpackCookie(request);
String fileName = null;
String realPath = com.joytrav.Constant.FILE_REAL_PATH
+ com.joytrav.Constant.USER_UPLOAD_DIR;
if (!file.isEmpty()) {
String fileType = FileUtils.getFileType(file.getOriginalFilename());
String name = RandomUtils.getRandomFileName();
fileName = name + “.” + fileType;
byte[] bytes = file.getBytes();
String newFileName = realPath + File.separator + fileName;
FileOutputStream fos = new FileOutputStream(newFileName);
fos.write(bytes); // 写入文件
fos.close();
File newFile = new File(newFileName);
if (newFile.isFile()) {
String url = com.joytrav.Constant.FILE_DOMAIN
+ com.joytrav.Constant.USER_UPLOAD_URL + “/”
+ fileName;
return “{‘url’:'” + url + “‘}”;
}
}
return null;
}
[1][2]
送给中意的TA,背面写上:某年某月某日,