uploadUtils

xiaoxiao2021-02-28  125

package com.htht.qht.utils; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import org.springframework.web.multipart.MultipartFile; public class UploadUtils { public static String getPropertiesValue(String propertyName) { Properties properties = PropertiesUtil.getInstance() .getProperties("resources/env.properties"); String value = properties.getProperty(propertyName); return value; } /** * 描述:返回一个根据当前时间生成的文件名 * @param originName * @return */ public static String getCurrentTimeFileName(String originName){ String timespan = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+1;// 当前时间转化为yyyymmddhhmmss; String ext = originName.substring(originName.lastIndexOf(".") + 1); String targetFileName =timespan + "."+ ext; return targetFileName; } /** * 返回上传目标文件夹的路径(绝对路径) * @return */ public static String getFilePathByPropertyName(String propertyName){ return getPropertiesValue("imgServer.path")+getPropertiesValue(propertyName); } /** * 上传用户头像 * @param file * @param propertyName * @return */ public static String uploadUserPhoto(MultipartFile file){ String newFileName = UploadUtils.getCurrentTimeFileName(file.getOriginalFilename()); String targetFilePath = getFilePathByPropertyName("userphoto.img.path"); File folder = new File(targetFilePath); File targetFile = new File(targetFilePath+newFileName); if(!folder.exists()&&!folder.isDirectory()){ folder.mkdirs(); } try { file.transferTo(targetFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return getPropertiesValue("userphoto.img.path")+targetFile.getName(); }

}

springmvc中:

<!-- 上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <!-- 5M --> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000000" /> </bean> </beans>

java代码:

//保存个人用户信息 @RequestMapping(value = "savePersonInfo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8") @ResponseBody public String savePersonInfo(String education,@RequestParam(value = "userPhoto", required = false) MultipartFile file, HttpServletRequest req,HttpServletResponse respons) { respons.setHeader("Access-Control-Allow-Origin", "*"); Object obj = req.getSession().getAttribute("userId"); String userId = ""; if (obj != null) { userId = obj.toString(); } if (StringUtils.isNotBlank(userId)){ req.setAttribute("filePath", fileKey); String imgPath = null; if(file!=null&&!file.isEmpty()){ imgPath = UploadUtils.uploadUserPhoto(file); } return portalService.savePersonUserInfo(userId, education,imgPath); }else{ Gson gson = new Gson(); Map<String,Object> map = new HashMap<String,Object>(); map.put("success", false); map.put("code", "notLogin"); return gson.toJson(map); } }

转载请注明原文地址: https://www.6miu.com/read-38936.html

最新回复(0)