简体   繁体   English

如何使用Java Socket从服务器下载文件?

[英]How to download a file from a server using Java Socket?

I have an assignment about uploading and downloading a file to a server. 我有一份关于将文件上传和下载到服务器的任务。 I managed to do the uploading part using Java Sockets however I am having a hard time doing the downloading part. 我设法使用Java Sockets完成了上传部分,但是我却很难进行下载部分。 I should use Range: for downloading parellel. 我应该使用Range:来下载parellel。 In my request, I should have the Range: header. 在我的请求中,我应该具有Range:标头。 But I don't understand how I will receive the file with that HTTP GET request. 但是我不明白如何通过HTTP GET请求接收文件。 All the examples I have seen was about uploading a file. 我所看到的所有示例都是关于上传文件的。 I already did it. 我已经做到了 I can upload .exe, image, .pdf, anything and when I download them back (by my browser), there are no errors. 我可以上传.exe,图片,.pdf等任何内容,并且当我通过浏览器将它们下载回来时,没有任何错误。 Can you help me with the downloading part? 您能帮我下载一下吗? Can you give me an example beacuse I really didn't get it. 你能给我一个例子,因为我真的不明白。

You need to read the HTTP response from the same socket on which you put the request. 您需要从发出请求的同一套接字读取HTTP响应 As a starting point, just print it out. 首先,只需将其打印出来即可。 When you are familiar with it, start parsing it. 当您熟悉它时,就开始解析它。 The file will be in there. 该文件将在其中。

This doesn't directly answer your question, but it is (IMO) worth saying anyway ... 这并不能直接回答您的问题,但是(IMO)仍然值得一提...

If your homework assignment doesn't specifically tell you to use a socket directly, there are simpler, and better ways of doing HTTP file upload and download in Java: 如果您的作业没有明确告诉您直接使用套接字,那么可以使用Java中更简单,更好的方法来上传和下载HTTP文件:

  • Using java.net.URL.openConnection() on an "http:" url will give you an HttpURLConnection that you can use to make GET, PUT, POST and so on requests to the remote server. 在“ http:” URL上使用java.net.URL.openConnection()将为您提供一个HttpURLConnection ,可用于向远程服务器发出GET,PUT,POST等请求。 This takes care of the basic HTTP protocol stuff for you. 这将为您处理基本的HTTP协议。

  • The Apache HttpClient libraries do the same thing, but in a more sophisticated way, with more options and more hooks for things like handling content (including forms and MIME multiparts), connection and credential management, proxying and route finding and so on. Apache HttpClient库做同样的事情,但是以更复杂的方式,提供了更多选项和更多钩子,例如处理内容(包括表单和MIME多个部分),连接和凭据管理,代理和路由查找等。

If the aim of your homework exercise is to teach you practical ways to talk to remote servers, then using these classes is far more practical than trying to implement a subset of the HTTP protocol from the socket level up. 如果您的家庭作业目的是教您与远程服务器进行通讯的实用方法,那么使用这些类比尝试从套接字级别实现HTTP协议的子集要实用得多。

(Of course, the aim could be to give you a deeper understanding of the HTTP protocol at the "wire" level ... which would make your current approach the right one.) (当然,其目的可能是使您在“有线”级别更深入地了解HTTP协议……这将使您当前的方法正确。)

Thank you I did it. 谢谢,我做到了。 I used a byte array and read the input stream and wrote it to a file using fileinputstream and fileoutputstream 我使用字节数组读取输入流,并使用fileinputstream和fileoutputstream将其写入文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM