百度webuploader 上传 到 阿里 OSS
程序员文章站
2022-06-08 07:53:46
...
服务端签名后直传
https://help.aliyun.com/document_detail/31926.html?spm=5176.product31815.6.614.HI0PNh
图片使用
https://help.aliyun.com/document_detail/44688.html?spm=5176.doc44686.6.924.t5CtYr
java后台签名
package wrules; import java.io.IOException; import java.sql.Date; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; import com.aliyun.oss.OSSClient; import com.aliyun.oss.common.utils.BinaryUtil; import com.aliyun.oss.model.MatchMode; import com.aliyun.oss.model.PolicyConditions; @WebServlet(asyncSupported = true) public class PostObjectPolicy extends HttpServlet{ /** * */ private static final long serialVersionUID = 5522372203700422672L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String endpoint = "oss-cn-beijing.aliyuncs.com/"; String accessId = "修改"; String accessKey = "修改"; String bucket = "修改"; String dir = "user-dir"; if(null!= request.getParameter("dir")){ dir = request.getParameter("dir"); } String host = "http://" + bucket + "." + endpoint; OSSClient client = new OSSClient(endpoint, accessId, accessKey); try { long expireTime = 30; long expireEndTime = System.currentTimeMillis() + expireTime * 1000; Date expiration = new Date(expireEndTime); PolicyConditions policyConds = new PolicyConditions(); policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000); policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir); String postPolicy = client.generatePostPolicy(expiration, policyConds); byte[] binaryData = postPolicy.getBytes("utf-8"); String encodedPolicy = BinaryUtil.toBase64String(binaryData); String postSignature = client.calculatePostSignature(postPolicy); Map<String, String> respMap = new LinkedHashMap<String, String>(); respMap.put("accessid", accessId); respMap.put("policy", encodedPolicy); respMap.put("signature", postSignature); //respMap.put("expire", formatISO8601Date(expiration)); respMap.put("dir", dir); respMap.put("host", host); respMap.put("expire", String.valueOf(expireEndTime / 1000)); JSONObject ja1 = new JSONObject(respMap); System.out.println(ja1.toString()); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "GET, POST"); response(request, response, ja1.toString()); } catch (Exception e) { e.printStackTrace(); } } private void response(HttpServletRequest request, HttpServletResponse response, String results) throws IOException { String callbackFunName = request.getParameter("callback"); if (callbackFunName==null || callbackFunName.equalsIgnoreCase("")) response.getWriter().println(results); else response.getWriter().println(callbackFunName + "( "+results+" )"); response.setStatus(HttpServletResponse.SC_OK); response.flushBuffer(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
javascript全局参数
var objdata = { upfile_endpoint:'http://修改.oss-cn-beijing.aliyuncs.com',//上传地址 upfile_nametype:'random_name',//local_name random_name 上传文件的文件名类型 upfile_defaltdir:'upload/CCCC'//上传路径 多层 格式 upload/floder1/floder2 };
uploader.on('uploadBeforeSend', function (obj, data, headers) { //TODO 如果同一个页面上传多次 需要处理签名逻辑 不用每次都签名 $.ajax({ type : "post", url : "osssignuature", timeout : 10000, data : { "dir" : objdata.upfile_defaltdir }, success : function(str) { if (str) { try { var re = JSON.parse(str); objdata.os ssignature = { 'key' : re.dir, 'policy': re.policy, 'OSSAccessKeyId': re.accessid, 'success_action_status' : '200', //让服务端返回200,不然,默认会返回204 'signature': re.signature }; } catch (e) { alert("系统错误"); } } else { alert("结果为空"); } }, error : function(XMLHttpRequest, textStatus, errorThrown) { alert("ajax error"); }, complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数 if(status == 'timeout'){ alert('请求超时,请稍后再试!'); } }, async : false }); //赋值参数 data = $.extend(data,objdata.osssignature); //设置文件路径 data.key = data.key + "/" + calculate_object_name(data.name,objdata.upfile_nametype); obj.filepath = data.key; file.path = data.key; headers['Access-Control-Allow-Origin'] = "*"; });
上传完效果
源码下载地址
http://download.csdn.net/detail/qinghechaoge/9712694
上一篇: Java 实例 - 删除目录