如何在 scribe oauthrequest 中添加文件参数?
如标题中所述,
如果输入是文件,则 oauthRequest.addBodyParameter(key, value) 似乎不能很好地工作
我尝试执行以下操作以将文件强制转换为字符串,但无济于事:
File f = new File("xyz.png");
InputStream is = new FileInputStream(f);
int intValue = -1;
String value = "";
do {
intValue = is.read();
if (intValue != -1) {
char c = (char) intValue;
value += c;
}
} while (intValue != -1);
顺便说一句,我正在尝试以编程方式将图像上传到 Flickr(不确定是否有更直接的方法)
As stated in the title
It seems that oauthRequest.addBodyParameter(key, value) doesn't work so well if the input is a file
I tried to do the following to force the file into a string, but to no avail:
File f = new File("xyz.png");
InputStream is = new FileInputStream(f);
int intValue = -1;
String value = "";
do {
intValue = is.read();
if (intValue != -1) {
char c = (char) intValue;
value += c;
}
} while (intValue != -1);
By the way, I am attempting to upload an image to Flickr programmatically (not sure whether there is more straightforward way)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然
addBodyParameter
不起作用,因为您不想包含 body 参数,而是想创建一个多部分 http 请求。Scribe 并没有让你轻松做到这一点,原因是 API 支持文件上传并不常见,而其他库可以很好地做到这一点。 (当抄写员迁移到使用 apache commons http 时,事情对每个人来说都会更容易)
正如 @Chris 所说(+1),您可以并且被鼓励使用
addPayload
方法,但您需要自己创建多部分请求。对不起。<子>
免责声明:我是图书馆的作者。
Of course
addBodyParameter
wont work because you don't want to include a body parameter, you want to create a multipart http request.Scribe does not makes this easy on you, and the reason is that it's not very common for Apis to support file uploads, and other libraries do this nicely. (When scribe is migrated to use apache commons http, things will be easier for everyone)
As @Chris says though (+1) you can and are encouraged to use the
addPayload
method, but you'll need to create the multipart request yourself. Sorry.Disclaimer: I'm the library author.
我认为您遇到的问题是如何读取图像文件。将图像表示为由一次读取一个字符构建的字符串会导致问题。 char 是字节,而 Java 中的字符串是 UTF-8。当您输出字符串时,您不仅会获得用于构建它的字符,还会获得 UTF-8 表示形式。
因此,我自己没有尝试过,您是否尝试过使用
public void addPayload(byte[] Payload)
方法而不是addBodyParameter(key, value)
?这似乎是立即采取行动的最佳时机。I think the issue you are having here is how you are reading in the image file. Representing an image as a String built from chars that you read in one at a time is causing the problem. While a char is a byte, Strings in Java are in UTF-8. When you output a String, you don't just get the chars you used to build it, you get the UTF-8 representation.
So not having tried this myself, have you tried using the
public void addPayload(byte[] payload)
method instead ofaddBodyParameter(key, value)
? That seems like the right away to do it.要补充 @Pablo 所说的内容,如果您已经在使用 Apache Commons HTTP 客户端库,则可以使用
MultipartEntity
对象来处理多部分请求格式:当然,这有一个缺点您需要将整个内容正文读入内存中的
byte[]
中,因此如果您要发送巨型文件,这可能不是最佳选择。然而,对于小上传,这对我来说很有效。To add onto what @Pablo said, if you are already using the Apache Commons HTTP client libraries, you can use your
MultipartEntity
object to handle the multipart request formatting:Of course, this has the downside of this is that you need to read the entire content body into a
byte[]
in memory, so if you're sending giant files this might not be optimal. However, for small uploads, this did the trick for me.