Commit 5e512023 by stylefeng

解决上传头像问题

parent 40369b81
......@@ -98,11 +98,11 @@ public class KaptchaController {
*/
@RequestMapping("/{pictureId}")
public void renderPicture(@PathVariable("pictureId") String pictureId, HttpServletResponse response) {
String path = gunsProperties.getFileUploadPath() + pictureId + ".jpg";
String path = gunsProperties.getFileUploadPath() + pictureId;
try {
byte[] bytes = FileUtil.toByteArray(path);
response.getOutputStream().write(bytes);
}catch (Exception e){
} catch (Exception e) {
//如果找不到图片就返回一个默认图片
try {
response.sendRedirect("/static/img/girl.gif");
......
......@@ -337,12 +337,13 @@ public class UserMgrController extends BaseController {
}
/**
* 上传图片(上传到项目的webapp/static/img)
* 上传图片
*/
@RequestMapping(method = RequestMethod.POST, path = "/upload")
@ResponseBody
public String upload(@RequestPart("file") MultipartFile picture) {
String pictureName = UUID.randomUUID().toString() + ".jpg";
String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename());
try {
String fileSavePath = gunsProperties.getFileUploadPath();
picture.transferTo(new File(fileSavePath + pictureName));
......
......@@ -408,7 +408,7 @@ public class ToolUtil {
* map的key转为小写
*
* @param map
* @return Map<String,Object>
* @return Map<String , Object>
*/
public static Map<String, Object> caseInsensitiveMap(Map<String, Object> map) {
Map<String, Object> tempMap = new HashMap<>();
......@@ -568,4 +568,15 @@ public class ToolUtil {
throw new RuntimeException(e);
}
}
/**
* 获取文件后缀名 不包含点
*/
public static String getFileSuffix(String fileWholeName) {
if (ToolUtil.isEmpty(fileWholeName)) {
return "none";
}
int lastIndexOf = fileWholeName.lastIndexOf(".");
return fileWholeName.substring(lastIndexOf + 1);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment