报错: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;
}
网友评论