P
phillip.s.powell
I am about to throw in the towel on Java at this point! Too many
questions!
How in the world do you do this one (again, in my native PHP it's
extremely easy:
$fileID = @fopen('/path/to/your/new/file', 'wb');
if ($fileID) {
@fputs($fileID,
file_get_contents('http://www.domain.com/someimage.jpg'));
@fclose($fileID);
}
That's it. Now, how on earth do I do that in Java???
I tried something like this but just fell apart on it:
/*
* FileDownloader.java
*
* Created on January 10, 2007, 3:47 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package FileTools;
import java.io.*;
import java.net.*;
/**
*
* @author ppowell-c
*/
public class FileDownloader implements Serializable {
/** Creates a new instance of FileDownloader */
public FileDownloader() {}
public static void download(URL url, File file) throws IOException
{
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
}
public static void toFile(File file, Object contents) throws
IOException {
FileOutputStream out = new FileOutputStream(file);
out.write(contents.getBytes());
}
}
This of course fails, but I have absolutely no idea how to get the
ImageIcon object "contents" to write into the file "/path/to/my/file".
Phil
questions!
How in the world do you do this one (again, in my native PHP it's
extremely easy:
$fileID = @fopen('/path/to/your/new/file', 'wb');
if ($fileID) {
@fputs($fileID,
file_get_contents('http://www.domain.com/someimage.jpg'));
@fclose($fileID);
}
That's it. Now, how on earth do I do that in Java???
I tried something like this but just fell apart on it:
/*
* FileDownloader.java
*
* Created on January 10, 2007, 3:47 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package FileTools;
import java.io.*;
import java.net.*;
/**
*
* @author ppowell-c
*/
public class FileDownloader implements Serializable {
/** Creates a new instance of FileDownloader */
public FileDownloader() {}
public static void download(URL url, File file) throws IOException
{
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
}
public static void toFile(File file, Object contents) throws
IOException {
FileOutputStream out = new FileOutputStream(file);
out.write(contents.getBytes());
}
}
This of course fails, but I have absolutely no idea how to get the
ImageIcon object "contents" to write into the file "/path/to/my/file".
Phil