这边写了个图片下载到ex模板的方法,本地调试的正常,为什么线上就不行了,希望大家解答一下
线上报500,java.io.ioexception: server returned http response code: 500 for url:
public byte[] downloadImage(String imageUrl) {
// //userfiles/附件//11/截图 2024-10-10 23115420241126205145.png
String fullUrl = null;
try {
// 对URL进行编码
// 分离路径和查询参数
int queryIndex = imageUrl.indexOf('?');
String baseUrl = (queryIndex == -1) ? imageUrl : imageUrl.substring(0, queryIndex);
String queryParams = (queryIndex == -1) ? "" : imageUrl.substring(queryIndex + 1);
// 对查询参数进行编码
String encodedQueryParams = URLEncoder.encode(queryParams, StandardCharsets.UTF_8.toString());
fullUrl = baseUrl + (queryParams.isEmpty() ? "" : "?" + encodedQueryParams);
// 替换空格为 %20
fullUrl = fullUrl.replace(" ", "%20");
// 创建URL对象并打开连接
URL url = new URL(fullUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// 检查响应码
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (InputStream in = connection.getInputStream()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
}
} else {
logger.error("下载图片失败: HTTP响应代码: " + responseCode + " for URL: " + fullUrl);
}
} catch (MalformedURLException e) {
logger.error("URL格式错误: " + fullUrl, e);
} catch (IOException e) {
logger.error("下载图片失败: " + fullUrl, e);
} catch (Exception e) {
logger.error("未知错误: " + fullUrl, e);
}
return null;
}