OSS Web直传问题
回 楼主正量网的帖子
官方的demo也有这个问题吗?说一下你用的浏览器和操作系统
-------------------------
Re回 楼主正量网的帖子
package com.demo.demo;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 com.aliyun.oss.OSSClient;import com.aliyun.oss.common.utils.BinaryUtil;import com.aliyun.oss.model.MatchMode;import com.aliyun.oss.model.PolicyConditions;import junit.framework.Assert;import net.sf.json.JSONObject;@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-hangzhou.aliyuncs.com'; String accessId = '***'; String accessKey = '***'; String bucket = '***'; String host = 'http://' + bucket + '.' + endpoint; OSSClient client = new OSSClient(endpoint, accessId, accessKey); try { //System.out.println(System.currentTimeMillis()); String dir = 'user-dir'; 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 respMap = new LinkedHashMap(); 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 = JSONObject.fromObject(respMap); //JSONArray ja1 = JSONArray.fromObject(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) { Assert.fail(e.getMessage()); } } 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); }}
上面的这个代码测试可用。
我觉得你可能是没有加:
response.setHeader('Access-Control-Allow-Origin', '*');response.setHeader('Access-Control-Allow-Methods', 'GET, POST');
赞0
踩0