Java电报:上传文件以发送sendAnimation [英] Telegram, Java: upload file to send in sendAnimation
问题描述
电报API 中有一种称为sendAnimation
的方法.有两个强制性参数:chat_id
和animation
. animation
的描述是这样的:
There's a method in Telegram API called sendAnimation
. There are two obligatory paramteres: chat_id
and animation
. animation
's descriptio is this:
类型:InputFile或String
Type: InputFile or String
说明:要发送的动画.传递file_id作为String来发送动画 存在于Telegram服务器上(建议),将HTTP URL作为 用于电报的字符串,以从互联网获取动画或上传 使用multipart/form-data的新动画.有关发送文件的更多信息»
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
类型的说明:
I have a local .gif
file that I want to send. So it looks like I need to use that multipart/form-data
method. I don't understand what that method is. I checked out the InputFile
type's description:
InputFile (对象),该对象表示要被保存的文件的内容. 已上传.必须以常规方式使用multipart/form-data发布 通过浏览器上传文件.
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
的东西,但不要写出确切的意思.
Again, they write about that multipart/form-data
thing, but don't write what exactly that is.
我以为我可以使用sendDocument
方法上传文件,但是上传的文档也必须为InputFile
类型.
I thought maybe I could upload a file using sendDocument
method, but the uploaded document must be of type InputFile
as well.
如何从本地.gif
中制作InputFile
对象?我可以将其转换为Java的InputStream
,仅此而已.
How do I make the InputFile
object out of my local .gif
? I can convert it to Java's InputStream
, but that's about it.
推荐答案
简单来说,multipart/form-data只是发送数据的一种加密类型,存在以下三种加密形式:
simply multipart/form-data is just an encryption type for the sent data there are three types of encryption in forms:
- application/x-www-form-urlencoded(默认)
- multipart/form-data
- 文本/纯文本
有关multipart/form-data的更多信息,请检查此链接
for more info about multipart/form-data check this link
我不知道Java中GIF对象的类型是什么,但让我们将其视为二进制文件,那么您可以使用POST请求按如下方式简单地发布此文本:
I don't know what is the type of your GIF object in java but let us consider it a binary file then you would simply post this text as follows using POST request:
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
这篇关于Java电报:上传文件以发送sendAnimation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!