HTTP PUT上传Java文件 [英] HTTP PUT to upload a file in Java
问题描述
编辑2:在OAuthService中添加了.debug(),并更新了输出。
原始问题:我正尝试使用 Vimeo API(流式方法)。我使用抄写员来授权我的应用程序,获取我的访问令牌并准备上传视频。在这个PUT的HTTP请求的示例中,我只是不知道该怎么做Vimeo API文档中提到的你的文件的二进制数据:
PUT http://1.2.3.4:8080/upload?ticket_id= abcdef124567890 HTTP / 1.1
主机:1.2.3.4:8080
内容长度:339108
内容类型:video / mp4
...您文件的二进制数据在这里... 。
我可以得到罚单和标题。只是,我该如何插入我的文件的二进制数据?
注意:
- 这是一个Java中的桌面应用程序
- 这是一个我正在测试的小视频文件
这是我的代码(请记住,我使用抄写)安装文件(第52行)
文件testUp = new File(C:/ Users / Kent / Desktop / test.mp4\" );
String contentLength = Long.toString(testUp.length());
System.out.println(内容长度是:+ contentLength);
byte [] fileBytes = ByteStreams.toByteArray(new FileInputStream(testUp));
//上传文件(第58行)
request = new OAuthRequest(Verb.PUT,endpoint);
request.addHeader(Content-Length,contentLength);
request.addHeader(Content-Type,video / mp4);
request.addPayload(fileBytes);
response = signSendAndPrint(service,accessToken,request,Upload PUT:+ endpoint);
//检查响应码是否有效(第65行)
if(response.getCode()!= 200){
System.out.println(响应不是200!它是:+ response.getCode());
return;
}
//验证上传(第71行)
request = new OAuthRequest(Verb.PUT,endpoint);
request.addHeader(Content-Length,0);
request.addHeader(Content-Range,bytes * / *);
response = signSendAndPrint(service,accessToken,request,Verify Upload PUT:+ endpoint);
//检查响应码是否有效(第77行)
if(response.getCode()!= 308){
System.out.println(响应不是308!这是:+ response.getCode());
return;
//完成上传(第83行)
request = new OAuthRequest(Verb.PUT,endpoint);
request.addQuerystringParameter(method,vimeo.videos.upload.complete);
响应completeResponse = signSendAndPrint(service,accessToken,request,complete); //这是第86行,这是我的代码的第二级,打破了这个过程。
//设置视频信息(第88行)
setVimeoVideoInfo(completeResponse,service,accessToken,vimeoAPIURL);
$ b / **
*签署服务请求,打印请求信息,发送请求,打印
*有关响应的信息并返回响应
*
* @param service
* @param accessToken
* @param请求
* @param描述
* @return
* /
私有静态响应signSendAndPrint(OAuthService服务,令牌accessToken,OAuthRequest请求,字符串描述){
service.signRequest(accessToken,request);
printRequest(request,description +Request);
Response response = request.send(); //这是第105行。这是我的代码的最高级别,打破了进程
printResponse(response,description +Response);
返回响应;
$ p的输出signSendAndPrint(OAuthService服务,令牌accessToken ,OAuthRequest请求,字符串描述)
方法和OAuthService.debug()
:上传PUT:http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e请求
标题:{Authorization = OAuth oauth_signature =MTPIVFfGVUQn4QswNV6av4CjzJw%3D,oauth_version = 1.0,oauth_nonce = - 606493399,oauth_signature_method =HMAC-SHA1,oauth_consumer_key =MY_CONSUMER_KEY,oauth_token =MY_OAUTH_TOKEN,oauth_timestamp =1332428103,Content-Length = 15125120,Content-Type = video / mp4 }
动词:PUT
完整的URL:http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e
上传PUT:http://174.129.155.54:8080 / upload?ticket_id = a64ed67b4aefdc35d18aec6cfa0b7c5e响应
代码:200
标题:{null = HTTP / 1.1 200 OK,Content-Length = 0,Connection = close,Content-Type = text / plain,Serve r = Vimeo / 1.0}
正文:
签名请求:http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e
将令牌设置为:令牌[87bddf1382ac9f423d4b7c4166bdf0b2,fdae7a033c7e1c932abce533627d6045124e8593]
生成签名...
基本字符串是:PUT& http%3A%2F%2F174.129.155.54%3A8080%2Fupload& oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D1585934110%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110 %26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%3Da64ed67b4aefdc35d18aec6cfa0b7c5e
签名是:bhWu7IX9JKEEn / ULcpJECEuwqOc =
附加的附加OAuth参数:{oauth_signature - > bhWu7IX9JKEEn / ULcpJECEuwqOc =,oauth_version - > 1.0,oauth_nonce - > 1585934110,oauth_signature_method - > HMAC-SHA1,oauth_consumer_key - > MY_CONSUMER_KEY,oauth_token - > MY_OAUTH_TOKEN,oauth_timestamp - > 1332428110}
使用Http标题签名
验证上传PUT:http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e请求
标题:{Authorization = OAuth oauth_signature = bhWu7IX9JKEEn%2FULcpJECEuwqOc%3D,oauth_version =1.0,oauth_nonce =1585934110,oauth_signature_method =HMAC-SHA1,oauth_consumer_key =MY_CONSUMER_KEY,oauth_token =MY_OAUTH_TOKEN,oauth_timestamp =1332428110,Content- 0,Content-Range = bytes * / *} * ///注意,这不是输出的一部分,我只是添加了* ///,所以剩下的部分不会被注释掉。
动词:PUT
完整的URL:http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e
验证上传PUT:http://174.129.155.54:8080 / upload?ticket_id = a64ed67b4aefdc35d18aec6cfa0b7c5e响应
代码:308
标题:{null = HTTP / 1.1 308恢复未完成,范围=字节= 0-15125119,内容长度= 0,连接=关闭, Type = text / plain,Server = Vimeo / 1.0}
正文:
签名请求:http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e&method=vimeo.videos.upload.complete
设置令牌为:令牌[87bddf1382ac9f423d4b7c4166bdf0b2,fdae7a033c7e1c932abce533627d6045124e8593]
产生签名...
基本字符串是:PUT& http%3A%2F%2F174.129.155.54%3A8080%2Fupload& method %3Dvimeo.videos.upload.complete%26oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D3111236130%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110%26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%物3Da 64ed67b4aefdc35d18aec6cfa0b7c5e
签名是:vXlQ6OUKms8eHan + wEBO2HXBn / M =
附加的附加OAuth参数:{oauth_signature - > vXlQ6OUKms8eHan + wEBO2HXBn / M =,oauth_version - > 1.0,oauth_nonce - > 3111236130,oauth_signature_method - > HMAC-SHA1,oauth_consumer_key - > MY_CONSUMER_KEY,oauth_token - > MY_OAUTH_TOKEN,oauth_timestamp - > 1332428110}
使用Http标题签名
完成请求
标题:{Authorization = OAuth oauth_signature =vXlQ6OUKms8eHan%2BwEBO2HXBn%2FM%3D,oauth_version =1.0,oauth_nonce = 3111236130,oauth_signature_method =HMAC-SHA1,oauth_consumer_key =MY_CONSUMER_KEY,oauth_token =MY_OAUTH_TOKEN,oauth_timestamp =1332428110}
动词:PUT
完整的URL:http://174.129。 155.54:8080 / upload?ticket_id = a64ed67b4aefdc35d18aec6cfa0b7c5e& method = vimeo.videos.upload.complete
线程main中的异常org.scribe.exceptions.OAuthException:创建连接时出现问题。
在org.scribe.model.Request.send(Request.java:70)
在org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
在autouploadermodel.VimeoTest .signSendAndPrint(VimeoTest.java:105)
在autouploadermodel.VimeoTest.main(VimeoTest.java:86)
引起:java.net.SocketException:意外的服务器结束
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:723)sun.net.www.http.HttpClient.parseHTTP $(HttpClient.java:589)
sun.net。 www.http.HttpClient.parseHTTPHeader(HttpClient.java:720)在sun.net.www.http.HttpClient.parseHTTP $(HttpClient.java:589)
sun.net.www.protocol。在HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
(org.scribe.model.Response) .java:28)
at org.scribe.model.Request.doSend(Request.java:110)
at org.scribe.model.Request.send(Re quest.java:62)
... 3 more
Java结果:1
解决方案完成上传时,您需要附加两个其他参数。 Vimeo API文档表示您需要vimeo.videos的三个参数。 upload.complete:文件名,oauth_token和ticket_id。抄写员为您处理oauth_token。
此外,您必须调用原始端点,而不是请求票证时获得的端点。Edit: I think I've figured out how to do the binary data part. Double check it in the code, but I'm pretty sure I've got it right. Now I'm getting a new error when trying to complete the upload as described in the Vimeo API documentation
Edit 2: Added .debug() to the OAuthService and updated the output.
Original Question: I'm trying to upload a video to Vimeo using the Vimeo API (Streaming Method). I'm using scribe to authorize my app, get my access token and prepare for the video upload. I just don't know what to do at the point the Vimeo API documentation says "binary data of your file here" in this example of a HTTP request for the PUT:
PUT http://1.2.3.4:8080/upload?ticket_id=abcdef124567890 HTTP/1.1
Host: 1.2.3.4:8080
Content-Length: 339108
Content-Type: video/mp4
.... binary data of your file here ....
I can get the ticket and headers fine. It's just, what do I do to insert the binary data of my file?
Notes:
- It's a desktop app in Java
- It's a small video file I'm testing
Here's my code for the put (remember, I'm using scribe)
// Setup File (line 52) File testUp = new File("C:/Users/Kent/Desktop/test.mp4"); String contentLength = Long.toString(testUp.length()); System.out.println("The content length is: " + contentLength); byte[] fileBytes = ByteStreams.toByteArray(new FileInputStream(testUp)); // Upload file (line 58) request = new OAuthRequest(Verb.PUT, endpoint); request.addHeader("Content-Length", contentLength); request.addHeader("Content-Type", "video/mp4"); request.addPayload(fileBytes); response = signSendAndPrint(service, accessToken, request, "Upload PUT: " + endpoint); // Check response code is valid (line 65) if (response.getCode() != 200) { System.out.println("The response was not 200! It was: " + response.getCode()); return; } // Verify the upload (line 71) request = new OAuthRequest(Verb.PUT, endpoint); request.addHeader("Content-Length", "0"); request.addHeader("Content-Range", "bytes */*"); response = signSendAndPrint(service, accessToken, request, "Verify Upload PUT: " + endpoint); // Check response code is valid (line 77) if (response.getCode() != 308) { System.out.println("The response was not 308! It was: " + response.getCode()); return; } // Complete Upload (line 83) request = new OAuthRequest(Verb.PUT, endpoint); request.addQuerystringParameter("method", "vimeo.videos.upload.complete"); Response completeResponse = signSendAndPrint(service, accessToken, request, "complete"); // This is line 86, it's the second to top level of my code that breaks the process. //Set video info (line 88) setVimeoVideoInfo(completeResponse, service, accessToken, vimeoAPIURL); } /** * Signs the request on the service, prints information on the request, sends the request, prints * information on the response and returns the response * * @param service * @param accessToken * @param request * @param description * @return */ private static Response signSendAndPrint(OAuthService service, Token accessToken, OAuthRequest request, String description) { service.signRequest(accessToken, request); printRequest(request, description + " Request"); Response response = request.send(); //This is line 105. It's the top level of my code that breaks the process printResponse(response, description + " Response"); return response; }
Output of the
signSendAndPrint(OAuthService service, Token accessToken, OAuthRequest request, String description)
method and on theOAuthService.debug()
:Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Request Headers: {Authorization=OAuth oauth_signature="MTPIVFfGVUQn4QswNV6av4CjzJw%3D", oauth_version="1.0", oauth_nonce="-606493399", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428103", Content-Length=15125120, Content-Type=video/mp4} Verb: PUT Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Response Code: 200 Headers: {null=HTTP/1.1 200 OK, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0} Body: signing request: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e setting token to: Token[87bddf1382ac9f423d4b7c4166bdf0b2 , fdae7a033c7e1c932abce533627d6045124e8593] generating signature... base string is: PUT&http%3A%2F%2F174.129.155.54%3A8080%2Fupload&oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D1585934110%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110%26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%3Da64ed67b4aefdc35d18aec6cfa0b7c5e signature is: bhWu7IX9JKEEn/ULcpJECEuwqOc= appended additional OAuth parameters: { oauth_signature -> bhWu7IX9JKEEn/ULcpJECEuwqOc= , oauth_version -> 1.0 , oauth_nonce -> 1585934110 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> MY_CONSUMER_KEY, oauth_token -> MY_OAUTH_TOKEN, oauth_timestamp -> 1332428110 } using Http Header signature Verify Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Request Headers: {Authorization=OAuth oauth_signature="bhWu7IX9JKEEn%2FULcpJECEuwqOc%3D", oauth_version="1.0", oauth_nonce="1585934110", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428110", Content-Length=0, Content-Range=bytes */*} *///Note, this is not part of the output, I just added */// so the rest of it doesn't appear commented out. Verb: PUT Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Verify Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Response Code: 308 Headers: {null=HTTP/1.1 308 Resume Incomplete, Range=bytes=0-15125119, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0} Body: signing request: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e&method=vimeo.videos.upload.complete setting token to: Token[87bddf1382ac9f423d4b7c4166bdf0b2 , fdae7a033c7e1c932abce533627d6045124e8593] generating signature... base string is: PUT&http%3A%2F%2F174.129.155.54%3A8080%2Fupload&method%3Dvimeo.videos.upload.complete%26oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D3111236130%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110%26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%3Da64ed67b4aefdc35d18aec6cfa0b7c5e signature is: vXlQ6OUKms8eHan+wEBO2HXBn/M= appended additional OAuth parameters: { oauth_signature -> vXlQ6OUKms8eHan+wEBO2HXBn/M= , oauth_version -> 1.0 , oauth_nonce -> 3111236130 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> MY_CONSUMER_KEY, oauth_token -> MY_OAUTH_TOKEN, oauth_timestamp -> 1332428110 } using Http Header signature complete Request Headers: {Authorization=OAuth oauth_signature="vXlQ6OUKms8eHan%2BwEBO2HXBn%2FM%3D", oauth_version="1.0", oauth_nonce="3111236130", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428110"} Verb: PUT Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e&method=vimeo.videos.upload.complete Exception in thread "main" org.scribe.exceptions.OAuthException: Problems while creating connection. at org.scribe.model.Request.send(Request.java:70) at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12) at autouploadermodel.VimeoTest.signSendAndPrint(VimeoTest.java:105) at autouploadermodel.VimeoTest.main(VimeoTest.java:86) Caused by: java.net.SocketException: Unexpected end of file from server at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:723) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:720) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1319) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468) at org.scribe.model.Response.<init>(Response.java:28) at org.scribe.model.Request.doSend(Request.java:110) at org.scribe.model.Request.send(Request.java:62) ... 3 more Java Result: 1
解决方案You need to append two other parameters when completing the upload. Vimeo API documentation says you need three parameters for vimeo.videos.upload.complete: filename, oauth_token and ticket_id. scribe takes care of the oauth_token for you.
Furthermore, you have to call the original endpoint, not the one obtained when requesting a ticket.
这篇关于HTTP PUT上传Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!