javaç¼ç¨æ¹æ³ä¸è½½æå¡å¨ä¸çæ件å°æ¬å°å®¢æ端ï¼ä»£ç å¦ä¸ï¼
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownLoad {
public static void downloadFile(URL theURL, String filePath) throws IOException {
File dirFile = new File(filePath);
if(!dirFile.exists()){
//æ件路å¾ä¸åå¨æ¶ï¼èªå¨å建ç®å½
dirFile.mkdir();
}
//ä»æå¡å¨ä¸è·åå¾ç并ä¿å
URLConnection connection = theURL.openConnection();
InputStream in = connection.getInputStream();
FileOutputStream os = new FileOutputStream(filePath+"\\123.png");
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = in.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
os.close();
in.close();
}
public static void main(String[] args) {
//ä¸é¢æ·»å æå¡å¨çIPå°åå端å£ï¼ä»¥åè¦ä¸è½½çæ件路å¾
String urlPath = "http://æå¡å¨IPå°å:端å£/image/123.png";
//ä¸é¢ä»£ç æ¯ä¸è½½å°æ¬å°çä½ç½®
String filePath = "d:\\excel";
URL url = new URL(urlPath);
try {
downloadFile(url,filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}