publicclasssolve{publicbooleansaveUrlAs(StringphotoUrl,StringfileName){try{URLurl=newURL(photoUrl);HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();Dat...
public class solve
{
public boolean saveUrlAs(String photoUrl, String fileName)
{
try
{
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
out.close();
in.close();
return true;
}
catch (Exception e)
{
return false;
}
}
public static void main(String arg[])
{
String photoUrl = "http://ad4.sina.com.cn/200601/12/43932_750450.jpg";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
//System.out.println(fileName);
solve ss = new solve();
String filePath = "d:/ghost/";
boolean flag = ss.saveUrlAs(photoUrl, filePath + fileName);
System.out.println("Run ok!\nGet URL file " + flag);
}
}
展开