在我的网络服务器中下载文件
我想实现一个功能,您发送照片的 URL,我的服务器会自动下载并将其存储在指定的文件夹中。
我研究了一些用例,但作为网络这一领域的初学者,我有点迷失。我考虑过 FTP,但并不完全是我想要的。
像这样,在我的网络服务上运行(使用Java + Tomcat + AXIS2),
void getPhoto(URL url){
//receive a photo and store at folder /photos
}
但是,我不知道有什么用,我一直在寻找一些httppost或httpget,我还应该以这种方式寻找吗?有一个虚拟样本,可以向我展示基本方法吗?
I would like to implement a function where you send a URL of a photo and my server will automatically download and store it in a specified folder.
I studied some use cases, but as a beginner in this area of the web, I was a bit lost. I thought about FTP but is not exactly what I want.
Like that, function on my webservice (using Java + Tomcat + AXIS2)
void getPhoto(URL url){
//receive a photo and store at folder /photos
}
but, I don't know what use, I was looking for some httppost or httpget, should I still looking for in this way? Has a dummie sample, to show me the basic way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不完全是“上传”,而只是“下载”。
只需在
URL
上调用openStream()
,您就拥有了一个可以执行任何操作的InputStream
。例如,写入FileOutputStream
。That's not exactly "uploading", but just "downloading".
Just call
openStream()
on theURL
and you've anInputStream
which you can do anything with. Writing to aFileOutputStream
for example.嘿使用这个代码来下载。
hey use this code to download.
您想查看使用 HttpURLConnection,称之为“connect”和“getInputStream”方法,不断从该流中读取数据并将数据写入文件,例如 FileOutputStream。
You want to look at using an HttpURLConnection, call it's 'connect' and 'getInputStream' methods, continually reading from that stream and writing that data to a file with e.g. a FileOutputStream.
要使用 URL 下载文件,作为其他人建议的替代方案,您可以查看 Apache Commons HttpClient。
还有一个写得很好的教程。
To download a file using a URL, as an alternative to what suggested by others, you can take a look to Apache Commons HttpClient.
There is also a well written tutorial.