小程序
小游戏
企业微信
微信支付
扫描小程序码分享
图片上传的Java该怎么写,需要调用小程序的什么东西吗,小程序上传上去的是一个临时路径和一个name,该怎么解析啊,在线等,急求
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
/** * 循环上传图片文件 发布咨询的时候 * * @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; }
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
前端提问还是后端提问,一下两个端扛不住呀
你的问题关键在于,你不会用Java
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
/**
* 循环上传图片文件 发布咨询的时候
*
* @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;
}
前端提问还是后端提问,一下两个端扛不住呀
你的问题关键在于,你不会用Java