| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,4 +8,5 @@ out | |||
| 8 | 8 | gen | |
| 9 | 9 | # Created by .ignore support plugin (hsz.mobi) | |
| 10 | 10 | target | |
| 11 | - framework | ||
| 11 | + framework | ||
| 12 | + images | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,5 +18,5 @@ public interface WebDefaultValueConst { | |||
| 18 | 18 | ||
| 19 | 19 | String defaultImage = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"; | |
| 20 | 20 | ||
| 21 | - String imgBaseUrl = System.getProperty("user.dir") + "/images/"; | ||
| 21 | + String imgBaseUrl = System.getProperty("user.dir") + "\\images\\"; | ||
| 22 | 22 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,18 +2,13 @@ | |||
| 2 | 2 | ||
| 3 | 3 | import info.xiaomo.core.constant.FileType; | |
| 4 | 4 | import info.xiaomo.core.constant.Symbol; | |
| 5 | - import org.apache.commons.fileupload.FileItem; | ||
| 6 | - import org.apache.commons.fileupload.FileItemFactory; | ||
| 7 | - import org.apache.commons.fileupload.disk.DiskFileItemFactory; | ||
| 8 | - import org.apache.commons.fileupload.servlet.ServletFileUpload; | ||
| 5 | + import info.xiaomo.core.constant.WebDefaultValueConst; | ||
| 9 | 6 | import org.springframework.web.multipart.MultipartFile; | |
| 10 | 7 | ||
| 11 | - import javax.servlet.http.HttpServletRequest; | ||
| 12 | 8 | import java.io.*; | |
| 13 | 9 | import java.net.MalformedURLException; | |
| 14 | 10 | import java.net.URL; | |
| 15 | 11 | import java.util.ArrayList; | |
| 16 | - import java.util.List; | ||
| 17 | 12 | ||
| 18 | 13 | /** | |
| 19 | 14 | * 此类中封装一些常用的文件操作。 | |
@@ -559,18 +554,6 @@ public static boolean genModuleTpl(String path, String modulecontent) throws IOE | |||
| 559 | 554 | return true; | |
| 560 | 555 | } | |
| 561 | 556 | ||
| 562 | - | ||
| 563 | - /** | ||
| 564 | - * 拼接新的文件名 | ||
| 565 | - * | ||
| 566 | - * @param baseName | ||
| 567 | - * @param fileType | ||
| 568 | - * @return | ||
| 569 | - */ | ||
| 570 | - public static String getNewFileName(String baseName, String fileType) { | ||
| 571 | - return baseName + Symbol.DIAN + fileType; | ||
| 572 | - } | ||
| 573 | - | ||
| 574 | 557 | /** | |
| 575 | 558 | * 获取图片文件的扩展名(发布系统专用) | |
| 576 | 559 | * | |
@@ -694,39 +677,37 @@ private static void list(ArrayList list, File file, | |||
| 694 | 677 | ||
| 695 | 678 | /** | |
| 696 | 679 | * 文件上传 | |
| 697 | - * | ||
| 698 | - * @param request | ||
| 699 | - * @param response | ||
| 700 | - * @param savePath | ||
| 701 | - */ | ||
| 702 | - public static String upload(HttpServletRequest request, MultipartFile multipartFile) { | ||
| 703 | - try { | ||
| 704 | - FileItemFactory factory = new DiskFileItemFactory(); | ||
| 705 | - ServletFileUpload upload = new ServletFileUpload(factory); | ||
| 706 | - upload.setHeaderEncoding("utf8");//支持中文文件名 | ||
| 707 | - List<FileItem> items = upload.parseRequest(request); | ||
| 708 | - for (FileItem item : items) { | ||
| 709 | - if (item.isFormField()) { | ||
| 710 | - System.out.println("查找到一个普通文本数据"); | ||
| 711 | - System.out.println("该文本数据的name为:" + item.getFieldName()); | ||
| 712 | - System.out.println("该文本数据的value为:" + item.getString()); | ||
| 713 | - System.out.println(); | ||
| 714 | - } else { | ||
| 715 | - System.out.println("查找到一个二进制数据"); | ||
| 716 | - System.out.println("该文件表单name为:" + item.getFieldName()); | ||
| 717 | - System.out.println("该文件文件名为:" + item.getName()); | ||
| 718 | - System.out.println("该文件文件类型为:" + item.getContentType()); | ||
| 719 | - System.out.println("该文件文件大小为:" + item.getSize()); | ||
| 720 | - System.out.println(); | ||
| 721 | - File uploadedFile = new File(request.getServletContext().getRealPath("fileupload") + "\\" + item.getName()); | ||
| 722 | - item.write(uploadedFile); | ||
| 680 | + * @param file file | ||
| 681 | + * @param email email | ||
| 682 | + * @return fileUrl | ||
| 683 | + */ | ||
| 684 | + public static String upload(MultipartFile file, String email) { | ||
| 685 | + String savePath = ""; | ||
| 686 | + if (file != null && !file.isEmpty()) { | ||
| 687 | + // 获取图片的文件名 | ||
| 688 | + String fileName = file.getOriginalFilename(); | ||
| 689 | + // 重新定义图片名字 | ||
| 690 | + String filename = FileUtil.getNewFileName(fileName,email); | ||
| 691 | + //上传服务器上 新文件路径 | ||
| 692 | + savePath = WebDefaultValueConst.imgBaseUrl; | ||
| 693 | + try { | ||
| 694 | + File newFile; | ||
| 695 | + // 判断服务器上 文件夹是否存在 | ||
| 696 | + newFile = new File(savePath); | ||
| 697 | + if (!newFile.exists()) { | ||
| 698 | + newFile.mkdirs(); | ||
| 723 | 699 | } | |
| 700 | + savePath = savePath + filename; | ||
| 701 | + FileOutputStream out = new FileOutputStream(savePath); | ||
| 702 | + // 写入文件 | ||
| 703 | + out.write(file.getBytes()); | ||
| 704 | + out.flush(); | ||
| 705 | + out.close(); | ||
| 706 | + } catch (Exception e) { | ||
| 707 | + e.printStackTrace(); | ||
| 724 | 708 | } | |
| 725 | - } catch (Exception e) { | ||
| 726 | - e.printStackTrace(); | ||
| 727 | 709 | } | |
| 728 | - | ||
| 729 | - return null; | ||
| 710 | + return savePath; | ||
| 730 | 711 | } | |
| 731 | 712 | ||
| 732 | 713 | ||
@@ -846,8 +827,8 @@ private static String getFile(String filePath) throws IOException { | |||
| 846 | 827 | } | |
| 847 | 828 | ||
| 848 | 829 | /** | |
| 849 | - * @param filePath | ||
| 850 | - * @return | ||
| 830 | + * @param filePath filePath | ||
| 831 | + * @return FileType | ||
| 851 | 832 | * @throws IOException | |
| 852 | 833 | */ | |
| 853 | 834 | public static FileType getType(String filePath) throws IOException { | |
@@ -865,6 +846,13 @@ public static FileType getType(String filePath) throws IOException { | |||
| 865 | 846 | return null; | |
| 866 | 847 | } | |
| 867 | 848 | ||
| 849 | + public static String getNewFileName(String fileName, String email) { | ||
| 850 | + String FileType = FileUtil.getFileType(fileName); | ||
| 851 | + String newName = email.split(Symbol.AT)[0]; | ||
| 852 | + return newName + Symbol.DIAN + FileType; | ||
| 853 | + } | ||
| 854 | + | ||
| 855 | + | ||
| 868 | 856 | public static void main(String args[]) throws Exception { | |
| 869 | 857 | System.out.println(getType("E:\\oscchina\\xiaomoBlogJava\\core\\src\\main\\java\\info\\xiaomo\\core\\constant\\GenderType.java")); | |
| 870 | 858 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,5 @@ | |||
| 1 | 1 | package info.xiaomo.web.controller; | |
| 2 | 2 | ||
| 3 | - import info.xiaomo.core.constant.Symbol; | ||
| 4 | - import info.xiaomo.core.constant.WebDefaultValueConst; | ||
| 5 | 3 | import info.xiaomo.core.controller.BaseController; | |
| 6 | 4 | import info.xiaomo.core.exception.UserNotFoundException; | |
| 7 | 5 | import info.xiaomo.core.model.UserModel; | |
@@ -17,8 +15,6 @@ | |||
| 17 | 15 | import org.springframework.web.bind.annotation.RestController; | |
| 18 | 16 | import org.springframework.web.multipart.MultipartFile; | |
| 19 | 17 | ||
| 20 | - import javax.servlet.http.HttpServletRequest; | ||
| 21 | - import java.io.File; | ||
| 22 | 18 | import java.io.IOException; | |
| 23 | 19 | import java.text.ParseException; | |
| 24 | 20 | import java.util.Date; | |
@@ -91,7 +87,6 @@ public HashMap<String, Object> login(@RequestParam String email, @RequestParam S | |||
| 91 | 87 | */ | |
| 92 | 88 | @RequestMapping(value = "register", method = RequestMethod.POST) | |
| 93 | 89 | public HashMap<String, Object> register( | |
| 94 | - HttpServletRequest request, | ||
| 95 | 90 | @RequestParam String nickName, | |
| 96 | 91 | @RequestParam String password, | |
| 97 | 92 | @RequestParam String email, | |
@@ -100,23 +95,22 @@ public HashMap<String, Object> register( | |||
| 100 | 95 | @RequestParam long phone, | |
| 101 | 96 | @RequestParam String address | |
| 102 | 97 | ) throws Exception { | |
| 103 | - // UserModel userModel = service.findUserByEmail(email); | ||
| 104 | - //邮箱被占用 | ||
| 105 | - // if (userModel != null) { | ||
| 106 | - // result.put(code, repeat); | ||
| 107 | - // return result; | ||
| 108 | - // } | ||
| 98 | + UserModel userModel = service.findUserByEmail(email); | ||
| 99 | + // 邮箱被占用 | ||
| 100 | + if (userModel != null) { | ||
| 101 | + result.put(code, repeat); | ||
| 102 | + return result; | ||
| 103 | + } | ||
| 109 | 104 | //目标文件名 | |
| 110 | - String imgUrl = FileUtil.upload(request,img); | ||
| 111 | - UserModel userModel = new UserModel(); | ||
| 105 | + String imgUrl = FileUtil.upload(img, email); | ||
| 106 | + userModel = new UserModel(); | ||
| 112 | 107 | userModel.setNickName(nickName); | |
| 113 | 108 | userModel.setEmail(email); | |
| 114 | 109 | userModel.setGender(gender); | |
| 115 | 110 | userModel.setImgUrl(imgUrl); | |
| 116 | 111 | userModel.setValidateStatus(0);//默认未验证 | |
| 117 | 112 | userModel.setValidateCode(MD5Util.encode(email)); | |
| 118 | 113 | userModel.setPhone(phone); | |
| 119 | - userModel.setImgUrl(WebDefaultValueConst.defaultImage); | ||
| 120 | 114 | userModel.setAddress(address); | |
| 121 | 115 | userModel.setPassword(MD5Util.encode(password)); | |
| 122 | 116 | userModel = service.addUser(userModel); | |
@@ -182,7 +176,6 @@ public HashMap<String, Object> findUserById(@RequestParam("id") Long id) { | |||
| 182 | 176 | */ | |
| 183 | 177 | @RequestMapping(value = "update", method = RequestMethod.POST) | |
| 184 | 178 | public HashMap<String, Object> update( | |
| 185 | - HttpServletRequest request, | ||
| 186 | 179 | @RequestParam String nickName, | |
| 187 | 180 | @RequestParam String password, | |
| 188 | 181 | @RequestParam String email, | |
@@ -197,22 +190,14 @@ public HashMap<String, Object> update( | |||
| 197 | 190 | result.put(code, error); | |
| 198 | 191 | return result; | |
| 199 | 192 | } | |
| 200 | - String OldFileName = img.getName(); | ||
| 201 | - String fileType = OldFileName.split(Symbol.DIAN)[1]; | ||
| 202 | - String newFileName = email + Symbol.DIAN + fileType; | ||
| 203 | - File targetFile = new File(WebDefaultValueConst.imgBaseUrl + Symbol.ZHENGXIEXIAN + newFileName, newFileName); | ||
| 204 | - if (!targetFile.exists()) { | ||
| 205 | - targetFile.mkdirs(); | ||
| 206 | - } | ||
| 207 | - if (!targetFile.exists()) { | ||
| 208 | - targetFile.createNewFile(); | ||
| 209 | - } | ||
| 193 | + //目标文件名 | ||
| 194 | + String imgUrl = FileUtil.upload(img, email); | ||
| 210 | 195 | userModel = new UserModel(); | |
| 211 | 196 | userModel.setNickName(nickName); | |
| 212 | 197 | userModel.setEmail(email); | |
| 213 | 198 | userModel.setGender(gender); | |
| 214 | 199 | userModel.setPhone(phone); | |
| 215 | - userModel.setImgUrl(""); | ||
| 200 | + userModel.setImgUrl(imgUrl); | ||
| 216 | 201 | userModel.setAddress(address); | |
| 217 | 202 | userModel.setPassword(MD5Util.encode(password)); | |
| 218 | 203 | userModel = service.updateUser(userModel); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments