美文网首页
oss图片上传不上去

oss图片上传不上去

作者: 一个神奇的女码农 | 来源:发表于2019-07-19 10:39 被阅读0次

报错:Caused by: java.io.IOException: Server returned HTTP response code: 403 for url
解决方法:
伪造一下UA就可以

 protected String downloadImg(String remoteSrcImg) throws Exception {
        String fileName = BaseGlobalConstants.ossArticleCollectDir + snowflakeIdService.newsFileNameNextId() + ".png";
        URL url = new URL(remoteSrcImg);
        HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
        httpUrlConn.setConnectTimeout(5000);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setRequestMethod("GET");
        httpUrlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
        // 获取输入流
        InputStream is = httpUrlConn.getInputStream();
        ObjectMetadata metadata = new ObjectMetadata();
        String s = uploadObject.putObject(fileName, is, metadata);
        return s;
    }

相关文章

网友评论

      本文标题:oss图片上传不上去

      本文链接:https://www.haomeiwen.com/subject/sghjlctx.html