在我的网络服务器中下载文件

发布于 2024-11-08 17:33:08 字数 355 浏览 0 评论 0原文

我想实现一个功能,您发送照片的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

才能让你更想念 2024-11-15 17:33:08

我想实现一个功能,您发送照片的 URL,我的服务器会自动下载并将其存储在指定文件夹中。

这不完全是“上传”,而只是“下载”。

只需在 URL 上调用 openStream(),您就拥有了一个可以执行任何操作的 InputStream。例如,写入 FileOutputStream

InputStream input = url.openStream();
// ...

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.

That's not exactly "uploading", but just "downloading".

Just call openStream() on the URL and you've an InputStream which you can do anything with. Writing to a FileOutputStream for example.

InputStream input = url.openStream();
// ...
你丑哭了我 2024-11-15 17:33:08

嘿使用这个代码来下载。

 try {



                URL url = new URL(url of file );
                URLConnection conection = url.openConnection();
                conection.connect();



                InputStream input = new BufferedInputStream(url.openStream());

                String downloadloc = "D:\"; // or anything

                OutputStream output = new FileOutputStream(downloadloc
                        + "\name of file.ext");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;



                    output.write(data, 0, count);
                }

                output.flush();

                output.close();
                input.close();

            } catch (Exception e) {

            }

hey use this code to download.

 try {



                URL url = new URL(url of file );
                URLConnection conection = url.openConnection();
                conection.connect();



                InputStream input = new BufferedInputStream(url.openStream());

                String downloadloc = "D:\"; // or anything

                OutputStream output = new FileOutputStream(downloadloc
                        + "\name of file.ext");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;



                    output.write(data, 0, count);
                }

                output.flush();

                output.close();
                input.close();

            } catch (Exception e) {

            }
远昼 2024-11-15 17:33:08

您想查看使用 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.

痴意少年 2024-11-15 17:33:08

要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文