百度webuploader上传到阿里OSS

百度webuploader 上传 到 阿里 OSS


服务端签名后直传

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后台签名

Java代码 
  1. package wrules;  
  2.   
  3. import java.io.IOException;  
  4. import java.sql.Date;  
  5. import java.util.LinkedHashMap;  
  6. import java.util.Map;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.annotation.WebServlet;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12.   
  13. import org.json.JSONObject;  
  14.   
  15. import com.aliyun.oss.OSSClient;  
  16. import com.aliyun.oss.common.utils.BinaryUtil;  
  17. import com.aliyun.oss.model.MatchMode;  
  18. import com.aliyun.oss.model.PolicyConditions;  
  19. @WebServlet(asyncSupported = true)  
  20. public class PostObjectPolicy extends HttpServlet{  
  21.     /** 
  22.      *  
  23.      */  
  24.     private static final long serialVersionUID = 5522372203700422672L;  
  25.   
  26.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
  27.       
  28.         String endpoint = "oss-cn-beijing.aliyuncs.com/";  
  29.         String accessId = "修改";  
  30.         String accessKey = "修改";  
  31.         String bucket = "修改";  
  32.           
  33.         String dir = "user-dir";  
  34.         if(null!= request.getParameter("dir")){  
  35.             dir = request.getParameter("dir");  
  36.         }  
  37.         String host = "http://" + bucket + "." + endpoint;  
  38.         OSSClient client = new OSSClient(endpoint, accessId, accessKey);  
  39.         try {     
  40.             long expireTime = 30;  
  41.             long expireEndTime = System.currentTimeMillis() + expireTime * 1000;  
  42.             Date expiration = new Date(expireEndTime);  
  43.             PolicyConditions policyConds = new PolicyConditions();  
  44.             policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 01048576000);  
  45.             policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);  
  46.   
  47.             String postPolicy = client.generatePostPolicy(expiration, policyConds);  
  48.             byte[] binaryData = postPolicy.getBytes("utf-8");  
  49.             String encodedPolicy = BinaryUtil.toBase64String(binaryData);  
  50.             String postSignature = client.calculatePostSignature(postPolicy);  
  51.               
  52.             Map<String, String> respMap = new LinkedHashMap<String, String>();  
  53.             respMap.put("accessid", accessId);  
  54.             respMap.put("policy", encodedPolicy);  
  55.             respMap.put("signature", postSignature);  
  56.             //respMap.put("expire", formatISO8601Date(expiration));  
  57.             respMap.put("dir", dir);  
  58.             respMap.put("host", host);  
  59.             respMap.put("expire", String.valueOf(expireEndTime / 1000));  
  60.             JSONObject ja1 = new JSONObject(respMap);  
  61.             System.out.println(ja1.toString());  
  62.             response.setHeader("Access-Control-Allow-Origin""*");  
  63.             response.setHeader("Access-Control-Allow-Methods""GET, POST");  
  64.             response(request, response, ja1.toString());  
  65.               
  66.         } catch (Exception e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.     }  
  70.       
  71.     private void response(HttpServletRequest request, HttpServletResponse response, String results) throws IOException {  
  72.         String callbackFunName = request.getParameter("callback");  
  73.         if (callbackFunName==null || callbackFunName.equalsIgnoreCase(""))  
  74.             response.getWriter().println(results);  
  75.         else  
  76.             response.getWriter().println(callbackFunName + "( "+results+" )");  
  77.         response.setStatus(HttpServletResponse.SC_OK);  
  78.         response.flushBuffer();  
  79.     }  
  80.       
  81.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    
  82.         doGet(request, response);  
  83.     }  
  84. }  


javascript全局参数

Javasrcipt代码 
  1. var objdata = {  
  2. upfile_endpoint:'http://修改.oss-cn-beijing.aliyuncs.com',//上传地址  
  3. upfile_nametype:'random_name',//local_name random_name  上传文件的文件名类型  
  4. upfile_defaltdir:'upload/CCCC'//上传路径 多层  格式  upload/floder1/floder2  
  5. };  

Javasrcipt代码 
  1. uploader.on('uploadBeforeSend', function (obj, data, headers) {  
  2.   //TODO   如果同一个页面上传多次  需要处理签名逻辑   不用每次都签名  
  3.     $.ajax({  
  4.          type : "post",  
  5.          url : "osssignuature",  
  6.         timeout : 10000,  
  7.         data : {  
  8.             "dir" : objdata.upfile_defaltdir  
  9.         },  
  10.         success : function(str) {  
  11.             if (str) {  
  12.                 try {  
  13.                     var re = JSON.parse(str);                                          objdata.os ssignature = {  
  14.                                         'key' : re.dir, 'policy': re.policy,  
  15.                                         'OSSAccessKeyId': re.accessid,   
  16.                                         'success_action_status' : '200', //让服务端返回200,不然,默认会返回204  
  17.                                         'signature': re.signature  
  18. };  
  19.                 } catch (e) {  
  20.                     alert("系统错误");  
  21.                 }  
  22.                   
  23.             } else {  
  24.                 alert("结果为空");  
  25.             }  
  26.         },  
  27.         error : function(XMLHttpRequest, textStatus, errorThrown) {  
  28.             alert("ajax error");  
  29.         },  
  30.         complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数  
  31.             if(status == 'timeout'){  
  32.                 alert('请求超时,请稍后再试!');  
  33.             }  
  34.         },  
  35.         async : false  
  36.     });  
  37.                       
  38.     //赋值参数  
  39.     data = $.extend(data,objdata.osssignature);  
  40.     //设置文件路径  
  41.     data.key = data.key + "/" + calculate_object_name(data.name,objdata.upfile_nametype);  
  42.     obj.filepath = data.key;  
  43.     file.path = data.key;  
  44.     headers['Access-Control-Allow-Origin'] = "*";  
  45. });  

源码下载地址
http://download.csdn.net/detail/qinghechaoge/9712694

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值