(1)引入文件上传的依赖。
commons-fileupload
commons-fileupload
1.4
(2) 创建一个页面
<%--
Created by IntelliJ IDEA.
User: 17162
Date: 2022/6/10
Time: 10:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--
method: 提交方式 文件上传必须为post提交。
enctype:默认application/x-www-form-urlencoded 表示提交表单数据
multipart/form-data:可以包含文件数据
input的类型必须为file类型,而且必须有name属性
--%>
(3)在springmvc中配置文件上传解析器
(4)创建upload01接口方法
@RequestMapping("upload03")
//注意:MultipartFile 参数名必须和中name属性相同
public String upload03(MultipartFile myfile,HttpServletRequest request) throws IOException {
// 1 得到本地服务目录的地址
String path = request.getSession().getServletContext().getRealPath("upload");
File file = new File(path);
System.out.println(file);
// 2 判断该目录是否存在
if(!file.exists()){
System.out.println(file);
file.mkdirs();
}
// 3 把myfile保存到本地服务中某个文件夹下
String filename =UUID.randomUUID().toString().replace("-","")+myfile.getOriginalFilename();
File target = new File(path+"/"+filename);
System.out.println(target);
// 4 把myfile转移到目标目录下
myfile.transferTo(target);
return "";
}
3.elementui+vue+axios完成文件上传
(1)页面的布局
<%--
Created by IntelliJ IDEA.
User: 17162
Date: 2022/6/10
Time: 9:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
(2)后台的接口
@RequestMapping("upload02")
@ResponseBody
public Map upload02(MultipartFile file, HttpServletRequest request){
try {
String path = request.getSession().getServletContext().getRealPath("upload");
File file1 = new File(path);
System.out.println(path);
if (!file1.exists()){
System.out.println(path);
file1.mkdirs();
}
String filename=UUID.randomUUID().toString().replace("-","")+file.getOriginalFilename();
File targer =new File(path+"/"+filename);
System.out.println(targer);
file.transferTo(targer);
System.out.println(targer);
Map map = new HashMap();
map.put("code",2000);
map.put("msg","上传成功");
map.put("data","http://localhost:8080//day0609_1_war/upload/"+filename);
return map;
} catch (IOException e) {
e.printStackTrace();
}
Map map = new HashMap();
map.put("code",5000);
map.put("msg","上传失败");
return map;
}
4.普通的文件上传到OSS文件服务器
(1)引入阿里云的OSS依赖
com.aliyun.oss aliyun-sdk-oss 3.10.2
(2)代码的书写。
@RequestMapping("/upload03")
public String upload03(MultipartFile myfile,HttpServletRequest request){
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
String endpoint = "oss-cn-hangzhou.aliyuncs.com";
//LTAI78XQAZq2s5Rv
//qdyZxR0x4LoUpTVbuyvCGdcrhEyw7H
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "LTAI78XQAZq2s5Rv";
String accessKeySecret = "qdyZxR0x4LoUpTVbuyvCGdcrhEyw7H";
// 填写Bucket名称,例如examplebucket。
String bucketName = "qy151";
//你上传到oss后的名字 会根据日期帮你创建文件夹。
//获取时间函数 以年月日区分文件夹
Calendar calendar=Calendar.getInstance();
String objectName =calendar.get(Calendar.YEAR)+"/"+(calendar.get(Calendar.MONTH)+1)+"/"+
calendar.get(Calendar.DATE)+"/"+UUID.randomUUID().toString().replace("-","")+
myfile.getOriginalFilename();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
InputStream inputStream =myfile.getInputStream();
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, inputStream);
} catch (Exception oe) {
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
//https://qy151.oss-cn-hangzhou.aliyuncs.com/2022/6/10/20d3d7e6b5bb455cb548675501f7270fgdnj.jpg
String url="https://"+bucketName+"."+endpoint+"/"+objectName;
return url;
}
5.保存用户信息–头像
1.前端
<%--
Created by IntelliJ IDEA.
User: 17162
Date: 2022/6/10
Time: 20:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
查询
2.后端工具类
package com.lzl.until;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.Calendar;
import java.util.UUID;
public class OSSutil {
public static String upload(MultipartFile file){
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
String endpoint = "oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "LTAI5tDYHmmTT5anrSgCZqGV";
String accessKeySecret = "CzYoKaYn5711KpvQ1rjbUJFFJzb1TS";
// 填写Bucket名称,例如examplebucket。
String bucketName = "liangmou";
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
String objectName = OSSutil.filename(file);
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
InputStream inputStream = file.getInputStream();
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, inputStream);
} catch (Exception oe) {
oe.printStackTrace();
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
// https://liangmou.oss-cn-hangzhou.aliyuncs.com/2022/6/1014f1edc98d7c435babd2a711c84b3be91.jpg
String path = "https://"+bucketName+"."+endpoint+"/"+objectName;
return path;
}
private static String filename(MultipartFile file){
Calendar calendar = Calendar.getInstance();
String fillname = calendar.get(Calendar.YEAR)+"/"+(calendar.get(Calendar.MONTH)+1)+
"/"+calendar.get(Calendar.DATE)+"/"+
UUID.randomUUID().toString().replace("-","")+
file.getOriginalFilename();
return fillname;
}
}
package com.lzl.until;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
private Integer code;
private String msg;
private Object data;
}
3.controller接口
package com.lzl.controller;
import com.lzl.entity.User;
import com.lzl.until.CommonResult;
import com.lzl.until.OSSutil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileController {
@RequestMapping("upload")
@ResponseBody
public CommonResult upload(MultipartFile file){
try {
String path = OSSutil.upload(file);
return new CommonResult(2000,"上传成功",path);
} catch (Exception e) {
e.printStackTrace();
}
return new CommonResult(2000,"上传失败",null);
}
@RequestMapping("upload01")
// @ResquesBody 将传过来的json 数据 转换为Java数据
public CommonResult upload01(@RequestBody User user){
System.out.println(user);
return new CommonResult(2000,"成功",null);
}
}



