使用的ssm框架 通过阿里云官网提供的Java sdk 做上传图片到专有云OSS 实现项目。 废话不多说
1、前端上传代码 jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="./resources/js/jquery.min.js"></script>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data">
<input id="file" type="file" name="file"/>
<br><br><br>
<button id="upload" type="button" οnclick="uploadImg()" >测试上传图片到阿里云</button>
</form>
<script>
/*
ajax文件上传教程:
https://blog.csdn.net/inuyasha1121/article/details/51915742
*/
function uploadImg() {
//debugger
$.ajax({
type: "post",
url: './upload',
data: new FormData($('#uploadForm')[0]),
processData: false,
contentType: false,
success: function (data) {
alert(data)
},
error:function (XMLHttpRequest, textStatus, errorThrown) {
alert("请求失败!");
}
});
}
</script>
<div style="float: left;">
<img src="<%=basePath%>IoReadImage" id="npcImg" width="125" height="148"/>
<input type="hidden" id="photo" name="photo"/>
</div>
</body>
</html>
controller
2、@Controller
public class OSSImageController {
@Autowired
private OSSImageService oSSImageService;
/**
* 前端上传图片
* @param file
* @return
* @throws Exception