Telegram, Java: 上传文件发送到sendAnimation
Telegram, Java: upload file to send in sendAnimation
Telegram API 中有一个方法叫做 sendAnimation
。有两个强制性参数:chat_id
和 animation
。 animation
的描述是这样的:
Type: InputFile or String
Description: Animation to send. Pass a file_id as String to send an animation that
exists on the Telegram servers (recommended), pass an HTTP URL as a
String for Telegram to get an animation from the Internet, or upload a
new animation using multipart/form-data. More info on Sending Files »
我有一个要发送的本地 .gif
文件。所以看起来我需要使用 multipart/form-data
方法。我不明白那个方法是什么。我查看了 InputFile
类型的描述:
InputFile This object represents the contents of a file to be
uploaded. Must be posted using multipart/form-data in the usual way
that files are uploaded via the browser.
同样,他们写了 multipart/form-data
东西,但不写它到底是什么。
我想也许我可以使用sendDocument
方法上传文件,但上传的文件也必须是InputFile
类型。
如何从本地 .gif
中创建 InputFile
对象?我可以将其转换为 Java 的 InputStream
,仅此而已。
简单来说multipart/form-data只是发送数据的一种加密方式,形式上有三种加密方式:
- application/x-www-form-urlencoded(默认值)
- multipart/form-data
- text/plain
有关 multipart/form-data 的更多信息,请查看此 link
我不知道 java 中你的 GIF 对象的类型是什么,但让我们将其视为一个二进制文件,然后你可以简单地 post 使用 [=33= 如下所示的文本] 请求:
String url = "uploading url";
String charset = "UTF-8";
String param = "value";
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
try {
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
// Send binary file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
Files.copy(binaryFile.toPath(), output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF).flush();
}
// Request is lazily fired whenever you need to obtain information about response.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode); // Should be 200
Telegram API 中有一个方法叫做 sendAnimation
。有两个强制性参数:chat_id
和 animation
。 animation
的描述是这样的:
Type: InputFile or String
Description: Animation to send. Pass a file_id as String to send an animation that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation from the Internet, or upload a new animation using multipart/form-data. More info on Sending Files »
我有一个要发送的本地 .gif
文件。所以看起来我需要使用 multipart/form-data
方法。我不明白那个方法是什么。我查看了 InputFile
类型的描述:
InputFile This object represents the contents of a file to be uploaded. Must be posted using multipart/form-data in the usual way that files are uploaded via the browser.
同样,他们写了 multipart/form-data
东西,但不写它到底是什么。
我想也许我可以使用sendDocument
方法上传文件,但上传的文件也必须是InputFile
类型。
如何从本地 .gif
中创建 InputFile
对象?我可以将其转换为 Java 的 InputStream
,仅此而已。
简单来说multipart/form-data只是发送数据的一种加密方式,形式上有三种加密方式:
- application/x-www-form-urlencoded(默认值)
- multipart/form-data
- text/plain
有关 multipart/form-data 的更多信息,请查看此 link
我不知道 java 中你的 GIF 对象的类型是什么,但让我们将其视为一个二进制文件,然后你可以简单地 post 使用 [=33= 如下所示的文本] 请求:
String url = "uploading url";
String charset = "UTF-8";
String param = "value";
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
try {
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
// Send binary file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
Files.copy(binaryFile.toPath(), output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF).flush();
}
// Request is lazily fired whenever you need to obtain information about response.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode); // Should be 200