0

I'm trying make a several uploads to my web server. I want to upload files that are at a specific web site, like all *.jpg files, so I started trying. this kind of code, using FTP, URL and Object File.

    UploaderDownloader up = new UploaderDownloader();
    URL url = new URL("http://i1.nyt.com/images/2011/05/22/magazine/22moth_cover/22moth_cover-moth.jpg");
    File file = new File(url.getFile()); 
    up.upload("127.0.0.1", "USER", "PASSWORD", "/testeUploader/132.jpg",file);

but this doesn't work for me. So, I'm looking to discover how implement this using the best choices. upload a url file that isn't at my PC to a web server.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So I take it that you need this in your specific Java program. Because, if you're simply FTPing files to a server, I strongly suggest using an FTP client such as FileZilla. If not, and you really need this in a Java program, please offer the documentation page for this UploaderDownloader class. I can't seem to find it. Also note that "127.0.0.1" is usually localhost. I'm sure you know this, just adding it in there. – Vinay May 22 '11 at 20:11
  • url.getFile() does not download anything – Jochen Bedersdorfer May 22 '11 at 21:10
  • Ok, I understand your questions, but my code is a just a sample that what I want. I'm implementing this program to automate my job. This is a link to UploaderDownloader class. [link](http://www.javabeat.net/tips/36-file-upload-and-download-using-java.html) – Clelio de Paula May 22 '11 at 22:23

2 Answers2

3

If I understand you right, you want to download a file from a HTTP server and then upload it to a FTP server. In that case you do not need the File object at all. It's only useful if you want to save it to the local disk file system, but you seem to not need this. All you need to do is to get an InputStream of the file from the HTTP server and then send it to the FTP server.

Here's a kickoff example how to get the file from the HTTP server in flavor of an InputStream.

InputStream input = new URL("http://example.com/image.jpg").openStream();

Simple isn't it? Please ensure that you respect the robots.txt of the site in question, or you might get IP-banned.

As to FTP'ing, the basic Java SE doesn't offer any useful API's for this and I have no idea what FTP client you're using since your question only contains homegrown and undocumented code, so here's just an example with Apache Commons Net FTPClient:

FTPClient ftp = new FTPClient();
ftp.connect("ftp.example.com");
ftp.login("username", "password");
ftp.storeFile("image.jpg", input); // <-- Here, it's just InputStream.
ftp.logout();

That's it.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, but seems that what I want is not usual, right?! Has a many web 2.0 apps, that you send a link to your picture and automaticaly the host server of web application record it. the best solution to solve this is the described above? – Clelio de Paula May 22 '11 at 22:42
  • I haven't said anywhere that it's not usual. Your question was only poorly formulated that I've made some guesses to understand what you *really* needed. The term "Upload URL" makes little sense, "HTTP files" also, saying "my web server" while you really meant "my FTP server" is also confusing, using `java.io.File` and a class with the name "UploaderDownloader" makes it also less clear. But now, is my answer now what you really needed? :) – BalusC May 22 '11 at 22:44
0

Where is the source for UploaderDownloader? Typically you can not perform a file upload of a file that is not at your PC without first downloading the file to your PC and then uploading it to the destination server.

It is possible using some FTP setups to do server-to-server transfers, or using one of the other available file transfer solutions like Dropbox or box.net, but generally what you're trying to do involves a local download first and then a file upload. For HTTP downloads and uploads Apache HttpClient is generally the library of choice: see How can I make a multipart/form-data POST request using Java? on the upload part, and http://hc.apache.org/httpcomponents-client-ga/tutorial/html/ for a general tutorial.

Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148
  • This is a link to UploaderDownloader class. [link](http://www.javabeat.net/tips/36-file-upload-and-download-using-java.html) – Clelio de Paula May 22 '11 at 22:34
  • Yeah, that class is for certain FTP setups: very different from HTTP uploads. What I think you're seeing is one site **linking** to images hosted on another (similar to what you see here on SO), but they aren't copying the image from the other server. – Femi May 23 '11 at 01:33