import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
/**
* 常用工具类
* 根据网络图片路径下载保存到本地
* @author 刘立
*
* 2014-11-21 上午10:16:10
*/
public class DownloadImags {
public String t1 (String url,HttpServletRequest request,String houzui) {
String filePath="DownLoad";
String savePath = request.getSession().getServletContext().getRealPath("/"+filePath);
File a=new File(savePath);
if (!a.exists()) {
// a.createNewFile();
a.mkdir();
}
// String str = "http://api.map.baidu.com/staticimage?center=106.720568,26.585137&width=697&height=550&markers=106.729443,26.593795&markerStyles=-1,http://api.map.baidu.com/images/marker_red.png,-1,23,25&zoom=15&labels=106.730143,26.594695&labelStyles=师大某小区包子铺,1,14,0xFFFFFF,0xEC624D,1";
// String str="http://image.c114.net/1yi.jpg";
String str=url;
DownloadImags dw=new DownloadImags();
String mingzi=AnguoFileUtils.getRandomFileName();
dw.saveToFile(str,savePath+"\\"+mingzi+houzui);
return mingzi+houzui;
}
/**
* 根据网络地址保存图片
* @param destUrl 网络地址
* @param filePath 图片存储路径
*/
public void saveToFile(String destUrl,String filePath) {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
int BUFFER_SIZE = 1024;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
try {
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
fos = new FileOutputStream(filePath);
while ((size = bis.read(buf)) != -1) {
fos.write(buf, 0, size);
}
fos.flush();
} catch (IOException e) {
} catch (ClassCastException e) {
} finally {
try {
fos.close();
bis.close();
httpUrl.disconnect();
} catch (IOException e) {
} catch (NullPointerException e) {
}
}
}
}
package com.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class download {
public static void DownloadFile(URL theURL, String filePath) throws IOException {
URLConnection con = theURL.openConnection();
String urlPath = con.getURL().getFile();
String fileFullName = urlPath.substring(urlPath.lastIndexOf("/") + 1);
if (fileFullName != null) {
byte[] buffer = new byte[4 * 1024];
int read;
String path = filePath + "/" + fileFullName;
File fileFolder = new File(filePath);
if(!fileFolder.exists()){
fileFolder.mkdir();
}
InputStream in = con.getInputStream();
FileOutputStream os = new FileOutputStream(path);
while ((read = in.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
os.close();
in.close()
;
long end = System.currentTimeMillis();
} else {
}
}
public static void main(String[] args) {
String urlS = "http://news.gdtone.com/ueditor/jsp/upload/file/20151210/1449730941714039415.pdf";
String filePathString = "d:/11";
URL url;
try {
url = new URL(urlS);
try {
DownloadFile(url,filePathString);
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}