图片上传的Java该怎么写,需要调用小程序的什么东西吗,小程序上传上去的是一个临时路径和一个name,该怎么解析啊,在线等,急求
/**
* 循环上传图片文件 发布咨询的时候
*
* @param orderInf
* @return
* @param orderInf
* @return
* @param userid
* state usertype comname comcode insurancedetail expertrate insuranceuser expertcompany title tags desc accessories username imagecontents accuratePrice conditions
* @return jsonobject
*/
@RequestMapping(value = “/xcxAddUploadFile”, method = RequestMethod.POST)
@ResponseBody
public JSONObject xcxAddUploadFile(HttpServletRequest request) {
JSONObject jsondata = new JSONObject();
try {
MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
// 对应前端的upload的name参数"file"
MultipartFile multipartFile = req.getFile(“file”);
String ordId = req.getParameter(“ordId”);
String userid = req.getParameter(“userid”);
String unionid = req.getParameter(“unionid”);
String filename = req.getParameter(“filename”);
System.out.println(ordId);
// 服务器项目路径
String pathRoot = request.getSession().getServletContext().getRealPath("");
String realPath = pathRoot.substring(0, pathRoot.lastIndexOf("\\")) + “\\insurancepic\\”;
// 取得图片的格式后缀
String originalLastName = multipartFile.getOriginalFilename();
String picLastName = originalLastName.substring(originalLastName.lastIndexOf("."));
// 拼接:名字+时间戳+后缀
String picName = UUID.generate() + picLastName;
String accessories = “/insurancepic/” + picName;
System.out.println(“accessories:” + accessories);
try {
File dir = new File(realPath);
// 如果文件目录不存在,创建文件目录
if (!dir.exists()) {
dir.mkdir();
System.out.println(“创建文件目录成功:” + realPath);
}
File file = new File(realPath, picName);
multipartFile.transferTo(file);
if (!StringUtils.isEmpty(ordId)) {
AdvisoryRecord advisoryRecord = new AdvisoryRecord();
advisoryRecord.setId(UUID.generate());
advisoryRecord.setUserid(userid);
advisoryRecord.setUnionid(unionid);
advisoryRecord.setOrdId(ordId);
advisoryRecord.setAccessories(accessories);
advisoryRecord.setType(ConstantUtils.RECORD_TYPE_ZERO);
if (filename == null || “”.equals(filename)) {
advisoryRecord.setContents(originalLastName);
} else {
advisoryRecord.setContents(filename);
}
// 增加新纪录
Boolean result = advisoryRecordDAO.addAdvisoryRecord(advisoryRecord);
// 如果增加失败
if (result == false) {
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ZERO);
jsondata.put(ConstantUtils.MSG, “提交失败”);
log.error(advisoryRecord.getOrdId() + “提交失败”);
return jsondata;
}
}
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ONE);
jsondata.put(ConstantUtils.MSG, ConstantUtils.SUCCESS);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ONE);
} catch (Exception e) {
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ZERO);
jsondata.put(ConstantUtils.MSG, “上传文件失败”);
log.error(“文件上传失败”, e);
return jsondata;
}
return jsondata;
}