开发者社区> 问答> 正文

OSS Web直传问题

java项目,跟官网DEMO一样 前端用的plupload组件 火狐浏览器没有问题 IE内核的浏览器每次打开后第一个上传的文件都会失败 关闭浏览器重新打开就又会失败一次
报这个错误

展开
收起
正量网 2015-12-15 10:55:53 10190 0
2 条回答
写回答
取消 提交回答
  • 回 1楼kkjuly的帖子
    官方的没有 不知道是哪里不对了 在win8.1,win7 上用IE8 IE11 chrome 猎豹 都试过 都一样的问题

    -------------------------

    回 3楼kkjuly的帖子
    谢谢 不过不是这个问题 加了也不对 错误是出在请求OSS这步 不懂为什么第一次不可以 第二次就好了
    2015-12-15 11:05:00
    赞同 展开评论 打赏
  • 回 楼主正量网的帖子
    官方的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<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 = 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");
    2015-12-15 11:01:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Web应用系统性能优化 立即下载
高性能Web架构之缓存体系 立即下载
PWA:移动Web的现在与未来 立即下载