Skip to content

Commit 89b4ce2

Browse files
committed
注册用户和更新用户
1 parent 5e86fe3 commit 89b4ce2

File tree

4 files changed

+52
-78
lines changed

4 files changed

+52
-78
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ out
88
gen
99
# Created by .ignore support plugin (hsz.mobi)
1010
target
11-
framework
11+
framework
12+
images

core/src/main/java/info/xiaomo/core/constant/WebDefaultValueConst.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ public interface WebDefaultValueConst {
1818

1919
String defaultImage = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
2020

21-
String imgBaseUrl = System.getProperty("user.dir") + "/images/";
21+
String imgBaseUrl = System.getProperty("user.dir") + "\\images\\";
2222
}

core/src/main/java/info/xiaomo/core/untils/FileUtil.java

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
import info.xiaomo.core.constant.FileType;
44
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;
96
import org.springframework.web.multipart.MultipartFile;
107

11-
import javax.servlet.http.HttpServletRequest;
128
import java.io.*;
139
import java.net.MalformedURLException;
1410
import java.net.URL;
1511
import java.util.ArrayList;
16-
import java.util.List;
1712

1813
/**
1914
* 此类中封装一些常用的文件操作。
@@ -559,18 +554,6 @@ public static boolean genModuleTpl(String path, String modulecontent) throws IOE
559554
return true;
560555
}
561556

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-
574557
/**
575558
* 获取图片文件的扩展名(发布系统专用)
576559
*
@@ -694,39 +677,37 @@ private static void list(ArrayList list, File file,
694677

695678
/**
696679
* 文件上传
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();
723699
}
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();
724708
}
725-
} catch (Exception e) {
726-
e.printStackTrace();
727709
}
728-
729-
return null;
710+
return savePath;
730711
}
731712

732713

@@ -846,8 +827,8 @@ private static String getFile(String filePath) throws IOException {
846827
}
847828

848829
/**
849-
* @param filePath
850-
* @return
830+
* @param filePath filePath
831+
* @return FileType
851832
* @throws IOException
852833
*/
853834
public static FileType getType(String filePath) throws IOException {
@@ -865,6 +846,13 @@ public static FileType getType(String filePath) throws IOException {
865846
return null;
866847
}
867848

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+
868856
public static void main(String args[]) throws Exception {
869857
System.out.println(getType("E:\\oscchina\\xiaomoBlogJava\\core\\src\\main\\java\\info\\xiaomo\\core\\constant\\GenderType.java"));
870858
}

web/src/main/java/info/xiaomo/web/controller/UserController.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package info.xiaomo.web.controller;
22

3-
import info.xiaomo.core.constant.Symbol;
4-
import info.xiaomo.core.constant.WebDefaultValueConst;
53
import info.xiaomo.core.controller.BaseController;
64
import info.xiaomo.core.exception.UserNotFoundException;
75
import info.xiaomo.core.model.UserModel;
@@ -17,8 +15,6 @@
1715
import org.springframework.web.bind.annotation.RestController;
1816
import org.springframework.web.multipart.MultipartFile;
1917

20-
import javax.servlet.http.HttpServletRequest;
21-
import java.io.File;
2218
import java.io.IOException;
2319
import java.text.ParseException;
2420
import java.util.Date;
@@ -91,7 +87,6 @@ public HashMap<String, Object> login(@RequestParam String email, @RequestParam S
9187
*/
9288
@RequestMapping(value = "register", method = RequestMethod.POST)
9389
public HashMap<String, Object> register(
94-
HttpServletRequest request,
9590
@RequestParam String nickName,
9691
@RequestParam String password,
9792
@RequestParam String email,
@@ -100,23 +95,22 @@ public HashMap<String, Object> register(
10095
@RequestParam long phone,
10196
@RequestParam String address
10297
) 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+
}
109104
//目标文件名
110-
String imgUrl = FileUtil.upload(request,img);
111-
UserModel userModel = new UserModel();
105+
String imgUrl = FileUtil.upload(img, email);
106+
userModel = new UserModel();
112107
userModel.setNickName(nickName);
113108
userModel.setEmail(email);
114109
userModel.setGender(gender);
115110
userModel.setImgUrl(imgUrl);
116111
userModel.setValidateStatus(0);//默认未验证
117112
userModel.setValidateCode(MD5Util.encode(email));
118113
userModel.setPhone(phone);
119-
userModel.setImgUrl(WebDefaultValueConst.defaultImage);
120114
userModel.setAddress(address);
121115
userModel.setPassword(MD5Util.encode(password));
122116
userModel = service.addUser(userModel);
@@ -182,7 +176,6 @@ public HashMap<String, Object> findUserById(@RequestParam("id") Long id) {
182176
*/
183177
@RequestMapping(value = "update", method = RequestMethod.POST)
184178
public HashMap<String, Object> update(
185-
HttpServletRequest request,
186179
@RequestParam String nickName,
187180
@RequestParam String password,
188181
@RequestParam String email,
@@ -197,22 +190,14 @@ public HashMap<String, Object> update(
197190
result.put(code, error);
198191
return result;
199192
}
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);
210195
userModel = new UserModel();
211196
userModel.setNickName(nickName);
212197
userModel.setEmail(email);
213198
userModel.setGender(gender);
214199
userModel.setPhone(phone);
215-
userModel.setImgUrl("");
200+
userModel.setImgUrl(imgUrl);
216201
userModel.setAddress(address);
217202
userModel.setPassword(MD5Util.encode(password));
218203
userModel = service.updateUser(userModel);

0 commit comments

Comments
 (0)