使用头像上传功能完成注册
Controller层代码
public String ZC(String name, String phone, String username, String pwd, MultipartFile file)
throws IllegalStateException, IOException {
// 定义保存数据库的路径
String sqlpath = null;
// 定义文件保存的本地路径
String localPath = "D:\\xxx\\SSMDemo\\WebContent\\images\\";
// 定义 文件名
String filename = null;
// 判断文件是否为空
if (!file.isEmpty()) {
// 生成uuid作为文件名称
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
// 获得文件类型(可以判断如果不是图片,禁止上传)
String contentType = file.getContentType();
// 获得文件后缀名
String suffixName = contentType.substring(contentType.indexOf("/") + 1);
// 得到 文件名
filename = uuid + "." + suffixName;
// 文件保存路径
file.transferTo(new File(localPath + filename));
}
// 得到文件保存在本地的路径
String xxx = localPath