(3)创建一个jsp页面4.0.0 org.example spring071.0-SNAPSHOT war org.springframework spring-webmvc5.2.15.RELEASE junit junit4.13.2 test org.projectlombok lombok1.18.24 javax.servlet javax.servlet-api4.0.1 com.fasterxml.jackson.core jackson-databind2.13.2.2 commons-fileupload commons-fileupload1.4 com.aliyun.oss aliyun-sdk-oss3.10.2
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
(4)在springmvc中配置文件上传解析器
(5)创建upload01接口的方法 3.elementui+vue+axios完成文件上传 (1)使用element+vue创建前端页面 直接使用elementui官网的模板
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
(2)后台接口
4.普通文件上传到OSS文件服务器
找到文件流上传 ---->直接复制改编代码中的值
(1)导入相关依赖:
com.aliyun.oss
aliyun-sdk-oss
3.10.2
(2)测试代码:
@Controller
public class Upload04 {
@RequestMapping("upload04")
public String upload02(MultipartFile myfile, HttpServletRequest request){
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
String endpoint = "oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "***********";
String accessKeySecret = "***************";
// 填写Bucket名称,例如examplebucket。
String bucketName = "admmm";
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
Calendar calendar = Calendar.getInstance();
int year =calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DATE);
String objectName = year+"/"+month+"/"+day+"/"+
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 (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
String url ="http://"+bucketName+"."+endpoint+"/"+objectName;
request.setAttribute("imgUrl",url);
return "util01.jsp";
}
}
接下来代码为使用springmvc在前端页面上传图片到阿里云,然后会在另一个页面回显上传的图片
(1)前端代码


