/**  
 * Description: <类功能描述-必填>  需要在每个方法前添加业务描述,方便业务业务行为的BI工作
 * Copyright:   Copyright (c)2012  
 * Company:     wendellup
 * @author:     yuchao
 * @version:    1.0  
 * Create at:   2013-5-26 下午8:50:47
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2013-1-28   yuchao     1.0       如果修改了;必填  
 */
package spider;import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;import org.apache.log4j.Logger;/**
 * @author yuchao
 * 
 */
public class DownloadPage {
private static Logger logger = Logger.getLogger(DownloadPage.class); // 需要下载对应网页的url
private String urlStr; public DownloadPage(String urlStr) {
this.urlStr = urlStr;
} private void download() throws Exception {
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
logger.info("getAuthority: " + url.getAuthority());
logger.info("getDefaultPort: " + url.getDefaultPort());
logger.info("getFile: " + url.getFile());
logger.info("getHost: " + url.getHost());
logger.info("getPath: " + url.getPath());
logger.info("getProtocol: " + url.getProtocol());
logger.info("getQuery: " + url.getQuery());
logger.info("getRef: " + url.getRef());
logger.info("getUserInfo: " + url.getUserInfo());
logger.info("getContent: " + url.getContent()); Method[] methods = URLConnection.class.getMethods();
for (Method method : methods) {
try {
if (!(method.getName().startsWith("get"))) {
continue;
}
logger.info("---------------- begin --------------------");
logger.info("method name: " + method.getName());
logger.info("method getParameterTypes: "
+ method.getParameterTypes());
if (method.getParameterTypes().length == 0) {
Object obj = method.invoke(connection);
logger.info(obj);
}
logger.info("---------------- end --------------------");
} catch (Exception e) {
logger.error("*** " + e.getMessage() + " ***");
}
} connection = url.openConnection();
String contentType = connection.getContentType(); String type = contentType.substring(contentType.lastIndexOf("/") + 1,
contentType.indexOf(";")==-1 ? contentType.length() : contentType.indexOf(";"));
String fileName = url.getFile();
fileName += "." + type;
fileName = UUID.randomUUID()+"."+type;

InputStream is = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(fileName);
byte[] buff = new byte[1024];
int i = 0;
while ((i = is.read(buff)) != -1) {
fos.write(buff,0,i);
}
fos.close();
is.close();
} public static void main(String[] args) {
// DownloadPage downloadPage = new DownloadPage("http://www.baidu.com/");
DownloadPage downloadPage = new DownloadPage("http://d.pcs.baidu.com/file/ac51a3ff40746fbea38c6d56e4626e38?fid=3475027816-250528-2837597873&time=1369581091&sign=FDTAR-DCb740ccc5511e5e8fedcff06b081203-a7u5kcdAKK%2F48UXx3L9frFblzMY%3D&rt=sh&expires=8h&r=586362294&sh=1&response-cache-control=private");
try {
downloadPage.download();
} catch (Exception e) {
e.printStackTrace();
}
}}
可以通过connection.getContentLength()获取资源的长度
那资源的名称怎么获取呢?
求前辈指教。