まず、私は2日間以上インターネット上で修正を見つけようとしたことを言及する必要があるので、私を別の投稿にリンクさせたい場合はおそらくそれはすでに見られたでしょう。今度は問題:403画像URLにアクセスしようとするとエラーが発生する
私は画像をダウンロードしようとすると、いくつかのリンクでコードが動作し、いくつかのコードではエラーが発生します。例えば、このリンクはエラーを出します。 .discordapp.com):https://cdn.discordapp.com/attachments/235200958974394368/256888406645145602/output.jpg
コードを見る前に、私はそれが外部APIを使用していることを言及する必要がありますが、それでもほとんどのことを理解するでしょうし、私のURLは常に画像URLです。
これは私のコードです:
try {
url = new URL(event.getMessage().getContent().substring(12));
openConnection = url.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
openConnection.setRequestProperty ("Authorization", basicAuth);
openConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
openConnection.setRequestProperty("Request Method", "POST");
// openConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
openConnection.setRequestProperty("Content-Language", "en-US");
openConnection.setUseCaches(false);
openConnection.setDoInput(true);
openConnection.setDoOutput(true);
if(openConnection.getContentLength() > 8000000) {
event.getChannel().sendMessage(event.getAuthor().getAsMention()+" file size is too big.").queue();
check = false;
}
} catch (Exception e) {
event.getChannel().sendMessage("Couldn't create a connection to the link, please recheck the link.").queue();
check = false;
e.printStackTrace();
}
if(check) {
BufferedImage img = null;
try {
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1 != (n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
img = ImageIO.read(new ByteArrayInputStream(response));
} catch (Exception e) {
event.getChannel().sendMessage(event.getAuthor().getAsMention()+" couldn't read an image from this link.").queue();
e.printStackTrace();
}
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(0.1f);
File file = new File("output.jpg");
try {
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
writer.setOutput(new FileImageOutputStream(file));
writer.write(null, new IIOImage(img, null, null), jpegParams);
event.getChannel().sendFile(file, null).queue();
file.delete();
} catch (IOException e) {
event.getChannel().sendMessage("Couldn't create/send the output image.").queue();
e.printStackTrace();
}
}
そして、これはエラー(おそらく、このライン[23:21:06] [Fatal] [JDA]: One of the EventListeners had an uncaught exception
ダウンから何かを無視することができる)とのスタックトレースです:もちろん
` java.io.IOException: Server returned HTTP response code: 403 for URL: https://cdn.discordapp.com/attachments/235200958974394368/256888406645145602/output.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at discordBot.Jpegify.execute(Jpegify.java:73)
at discordBot.Christina.onMessageReceived(Christina.java:163)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:316)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:64)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleDefaultMessage(MessageCreateHandler.java:129)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:51)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:38)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:665)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:339)
at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:262)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:240)
at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:965)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:748)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:110)
at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:66)
[23:21:06] [Fatal] [JDA]: One of the EventListeners had an uncaught exception
[23:21:06] [Fatal] [JDA]: Encountered an exception:
[23:21:06] [Fatal] [JDA]: java.lang.IllegalArgumentException: image == null!
at javax.imageio.IIOImage.<init>(Unknown Source)
at discordBot.Jpegify.execute(Jpegify.java:99)
at discordBot.Christina.onMessageReceived(Christina.java:163)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:316)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:64)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleDefaultMessage(MessageCreateHandler.java:129)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:51)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:38)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:665)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:339)
at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:262)
at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:240)
at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:965)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:748)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:110)
at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:66)`
任意のヘルプ大いに感謝して、長いポストのために申し訳ありません、この問題はちょうど実際に私の神経に乗っています...
"User-Agent"にはさまざまなオプションがありましたが、現在はあなたのものは含まれていません。\ – user619361
今すぐ見てみてください。私はそれを編集し、それは私のために働いています –
@Sid ok、それは今、かなり意味があります.. Webサーバーは実際に 'User-Agent'プロパティをチェックしているようです。 –