- 浏览: 76115 次
- 性别:
- 来自: 广州
文章分类
最新评论
收藏列表
标题 | 标签 | 来源 | |
spring Batch实现数据库大数据量读写 | spring Batch实现数据库大数据量读写 | ||
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 1) USE ANNOTATIONS TO IDENTIFY AND WIRE SPRING BEANS. --> <context:component-scan base-package="net.etongbao.vasp.ac" /> <!-- 2) DATASOURCE, TRANSACTION MANAGER AND JDBC TEMPLATE --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" abstract="false" scope="singleton"> <!-- oracle.jdbc.driver.oracleDriver --> <property name="driverClass" value="oracle.jdbc.OracleDriver" /> <property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.1.23:1521:orcl01" /> <property name="user" value="USR_DEV01" /> <property name="password" value="2AF0829C" /> <property name="checkoutTimeout" value="30000" /> <property name="maxIdleTime" value="120" /> <property name="maxPoolSize" value="100" /> <property name="minPoolSize" value="2" /> <property name="initialPoolSize" value="2" /> <property name="maxStatements" value="0" /> <property name="maxStatementsPerConnection" value="0" /> <property name="idleConnectionTestPeriod" value="30" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans> |
|||
[转载]POI, iText export Excel & PDF | 利用poi,iText导出excel,pdf | ||
//创建ExcelEntity import java.io.OutputStream; import java.util.List; public class ExcelEntity<T> { private String title = "报表标题"; private String[] headers; private List<T> dataset; private OutputStream os; private boolean hasComment = false; private String commentContent = "report"; private String commentAuthor = "Galo"; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String[] getHeaders() { return headers; } public void setHeaders(String[] headers) { this.headers = headers; } public List<T> getDataset() { return dataset; } public void setDataset(List<T> dataset) { this.dataset = dataset; } public OutputStream getOs() { return os; } public void setOs(OutputStream os) { this.os = os; } public boolean isHasComment() { return hasComment; } public void setHasComment(boolean hasComment) { this.hasComment = hasComment; } public String getCommentContent() { return commentContent; } public void setCommentContent(String commentContent) { this.commentContent = commentContent; } public String getCommentAuthor() { return commentAuthor; } public void setCommentAuthor(String commentAuthor) { this.commentAuthor = commentAuthor; } } //创建PdfEntity import java.io.OutputStream; import java.util.List; public class PdfEntity<T> { private String title; //标题 private float margin_bottom = 50; //下边距 private float margin_left = 50; private float margin_top = 50; private float margin_right = 50; private String pageSize = "A4"; //纸张大小 private String fileName = "E://report.pdf"; //文件名称 private String author = "Galo"; //作者 private String subject = "Zhang"; //子项 private boolean creationDate = true; //创建时间 private String creator = "Galo"; //创建者 private String keywords = "报表"; //关键字 private String pageHeader; //页眉 private String pageFooter; //页脚 private String[] headers; //表头 private List<T> dataset; //数据集 private OutputStream os; //文件输出流 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public float getMargin_bottom() { return margin_bottom; } public void setMargin_bottom(float margin_bottom) { this.margin_bottom = margin_bottom; } public float getMargin_left() { return margin_left; } public void setMargin_left(float margin_left) { this.margin_left = margin_left; } public float getMargin_top() { return margin_top; } public void setMargin_top(float margin_top) { this.margin_top = margin_top; } public float getMargin_right() { return margin_right; } public void setMargin_right(float margin_right) { this.margin_right = margin_right; } public String getPageSize() { return pageSize; } public void setPageSize(String pageSize) { this.pageSize = pageSize; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public boolean isCreationDate() { return creationDate; } public void setCreationDate(boolean creationDate) { this.creationDate = creationDate; } public String getCreator() { return creator; } public void setCreator(String creator) { this.creator = creator; } public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public String getPageHeader() { return pageHeader; } public void setPageHeader(String pageHeader) { this.pageHeader = pageHeader; } public String getPageFooter() { return pageFooter; } public void setPageFooter(String pageFooter) { this.pageFooter = pageFooter; } public String[] getHeaders() { return headers; } public void setHeaders(String[] headers) { this.headers = headers; } public List<T> getDataset() { return dataset; } public void setDataset(List<T> dataset) { this.dataset = dataset; } public OutputStream getOs() { return os; } public void setOs(OutputStream os) { this.os = os; } } //下面是具体导出方法 import java.awt.Color; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFComment; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import com.jd.report.entity.ExcelEntity; import com.jd.report.entity.PdfEntity; import com.jd.report.util.PdfParagraph; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.HeaderFooter; import com.lowagie.text.PageSize; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class ExportUtils<T> { @SuppressWarnings("unchecked") public void exportPdf(PdfEntity<T> pdfEntity){ String pageSize = pdfEntity.getPageSize(); Rectangle rectangle = new Rectangle(getPdfPageSize(pageSize)); Document document = new Document(rectangle,pdfEntity.getMargin_bottom(),pdfEntity.getMargin_left(),pdfEntity.getMargin_right(),pdfEntity.getMargin_top()); try { PdfWriter.getInstance(document, pdfEntity.getOs()); //基本配置信息 document.addTitle(pdfEntity.getTitle()); document.addAuthor(pdfEntity.getAuthor()); if(pdfEntity.isCreationDate()){ document.addCreationDate(); } document.addCreator(pdfEntity.getCreator()); document.addKeywords(pdfEntity.getKeywords()); document.addSubject(pdfEntity.getSubject()); //定义页眉和页脚 String pageHeader = pdfEntity.getPageHeader(); String pageFooter = pdfEntity.getPageFooter(); HeaderFooter header = null; HeaderFooter footer = null; if(pageHeader != null){ header = new HeaderFooter(new PdfParagraph(pageHeader,14,true),false); header.setBorderWidth(0); header.setAlignment(Element.ALIGN_CENTER); } if(pageFooter != null){ footer = new HeaderFooter(new PdfParagraph(pageFooter,14,true),false); footer.setBorderWidth(0); footer.setAlignment(Element.ALIGN_CENTER); } document.setHeader(header); document.setFooter(footer); //打开pdf文档 document.open(); String[] headers = pdfEntity.getHeaders(); //创建多少列的表格 PdfPTable table = new PdfPTable(headers.length); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.setWidthPercentage(16 * headers.length); //产生表格栏 PdfPCell cell = null; for (int i = 0; i < headers.length; i++) { cell = new PdfPCell(new PdfParagraph(headers[i], 14,true)); cell.setHorizontalAlignment(Cell.ALIGN_CENTER); cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); cell.setBackgroundColor(Color.cyan); cell.setBorderColor(Color.green); table.addCell(cell); } //装载数据行 Collection<T> dataset = pdfEntity.getDataset(); Iterator<T> it = dataset.iterator(); while(it.hasNext()){ T t = it.next(); Class tClass = t.getClass(); Field[] fields = tClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { String fieldName = fields[i].getName(); String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1); Method getMethod = tClass.getMethod(getMethodName, new Class[]{}); Object value = getMethod.invoke(t, new Object[]{}); value = value == null ? "" : value; if(value != null){ cell = new PdfPCell(new PdfParagraph(value.toString(),12,false)); cell.setHorizontalAlignment(Cell.ALIGN_CENTER); cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); cell.setBorderColor(Color.green); table.addCell(cell); } } } document.add(table); document.close(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings("unchecked") public void exportExcel(ExcelEntity excelEntity){ String title = excelEntity.getTitle(); HSSFWorkbook workbook = new HSSFWorkbook(); //生成一个表格 HSSFSheet sheet = workbook.createSheet(title); //设置默认列宽度为15个字节 sheet.setDefaultColumnWidth(15); //生成一个标题样式 HSSFCellStyle style = workbook.createCellStyle(); //设置居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置填充前景色和背景色 style.setFillForegroundColor(HSSFColor.YELLOW.index); style.setFillBackgroundColor(HSSFColor.WHITE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //设置线条宽度 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); //生成一个字体 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short)12); //字体应用到样式 style.setFont(font); //生成主体样式 HSSFCellStyle bodyStyle = workbook.createCellStyle(); //设置居中 bodyStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置填充前景色和背景色 bodyStyle.setFillForegroundColor(HSSFColor.WHITE.index); bodyStyle.setFillBackgroundColor(HSSFColor.WHITE.index); bodyStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //设置线条宽度 bodyStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); bodyStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); bodyStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); bodyStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); //生成一个字体 HSSFFont bodyFont = workbook.createFont(); bodyFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); bodyFont.setColor(HSSFColor.VIOLET.index); bodyFont.setFontHeightInPoints((short)12); //字体应用到样式 bodyStyle.setFont(bodyFont); HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,0,0,0,(short)4,2,(short)6,5)); comment.setString(new HSSFRichTextString(excelEntity.getCommentContent())); comment.setAuthor(excelEntity.getCommentAuthor()); //产生标题行 String[] headers = excelEntity.getHeaders(); int index = 0; HSSFRow row = sheet.createRow(index); HSSFCell cell = null; for (short i = 0; i < headers.length; i++) { cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } //遍历集合,产生数据行 Collection<T> dataset = excelEntity.getDataset(); Iterator<T> it = dataset.iterator(); while(it.hasNext()){ index++; row = sheet.createRow(index); T t = it.next(); Class tClass = t.getClass(); Field[] fields = tClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { cell = row.createCell(i); cell.setCellStyle(style); String fieldName = fields[i].getName(); String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1); try { Method getMethod = tClass.getMethod(getMethodName, new Class[]{}); Object value = getMethod.invoke(t, new Object[]{}); value = value == null ? "" : value; if(value != null){ HSSFRichTextString textString = new HSSFRichTextString(value.toString()); cell.setCellValue(textString); cell.setCellStyle(bodyStyle); } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } try { //写出excel workbook.write(excelEntity.getOs()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void exportWord(){ } public Rectangle getPdfPageSize(String pageSize){ Rectangle pSize = null; if("A4".equals(pageSize)){ pSize = PageSize.A4; }else if("A3".equals(pageSize)){ pSize = PageSize.A3; }else if("A2".equals(pageSize)){ pSize = PageSize.A2; }else if("A1".equals(pageSize)){ pSize = PageSize.A1; }else{ pSize = PageSize.A4; } return pSize; } } //对于pdf中文乱码,有如下辅助类 import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; import com.lowagie.text.Chunk; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.BaseFont; public class PdfParagraph extends Paragraph { private static final long serialVersionUID = -244970043180837974L; private static Properties pro ; private static InputStream is ; static{ try { is = PdfParagraph.class.getClassLoader().getResourceAsStream("fontSrc.properties"); pro = new Properties(); pro.load(is); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static String getPlan(String source){ return pro.getProperty(source); } public PdfParagraph(String content) { super(content, getChineseFont(12, false)); } public PdfParagraph(String content, int fontSize, boolean isBold) { super(content, getChineseFont(fontSize, isBold)); } // 设置字体-返回中文字体 protected static Font getChineseFont(int nfontsize, boolean isBold) { BaseFont bfChinese; Font fontChinese = null; try { bfChinese = BaseFont.createFont(getPlan("fontSrc") + ":\\windows\\fonts\\simsun.ttc,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); if (isBold) { fontChinese = new Font(bfChinese, nfontsize, Font.BOLD); } else { fontChinese = new Font(bfChinese, nfontsize, Font.NORMAL); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return fontChinese; } // 转化中文 protected Cell ChangeCell(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException { Phrase ph = ChangeChinese(str, nfontsize, isBold); Cell cell = new Cell(ph); // cell.setBorderWidth(3); return cell; } // 转化中文 protected Chunk ChangeChunk(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException { Font FontChinese = getChineseFont(nfontsize, isBold); Chunk chunk = new Chunk(str, FontChinese); return chunk; } // 转化中文 protected Phrase ChangeChinese(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException { Font FontChinese = getChineseFont(nfontsize, isBold); Phrase ph = new Phrase(str, FontChinese); return ph; } } } |
|||
HttpClient | HttpClient 代理实例(Get方式) | ||
import java.io.IOException; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.ProxyHost; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.http.client.ClientProtocolException; /** * @ClassName: ProxySample * @Description: 测试HttpClient代理 * @author * @date 2012-1-10 上午9:45:10 * @version V1.0 */ public class ProxySample { /** * @Title: main * @Description: 测试HttpClient代理 * @param args * @author * @date 2012-1-10 */ public static void main(String[] args) { // 创建 HttpClient 的实例 HttpClient httpClient = new HttpClient(); // 代理的主机 ProxyHost proxy = new ProxyHost("openproxy.huawei.com", 8080); // 使用代理 httpClient.getHostConfiguration().setProxyHost(proxy); // 创建Get连接方法的实例 HttpMethod getMethod = new GetMethod("http://www.apache.org"); // 使用系统提供的默认的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { // 请求URI System.out.println("executing request " + getMethod.getURI()); // 执行getMethod int status = httpClient.executeMethod(getMethod); System.out.println("status:" + status); // 连接返回的状态码 if (HttpStatus.SC_OK == status) { System.out.println("Connection to " + getMethod.getURI() + " Success!"); // 获取到的内容 String responseBody = getMethod.getResponseBodyAsString(); System.out.println(responseBody); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (URIException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 释放连接 getMethod.abort(); } } } |
|||
HttpClient | HttpClient 代理实例(Get方式) 线程 | ||
import java.io.IOException; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.ProxyHost; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; /** * @ClassName: ProxySample * @Description: 测试HttpClient代理 * @author * @date 2012-1-10 上午9:45:10 * @version V1.0 */ public class ProxySample { /** * @Title: main * @Description: 测试HttpClient代理 * @param args * @author * @date 2012-1-10 */ public static void main(String[] args) { String url = "http://www.apache.org"; new ProxySample().get(url); } /** * @Title: get * @Description: 根据输入的url,获取对应的内容 * @param url * @author * @date 2012-1-10 */ public void get(final String url) { MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager(); // 创建 HttpClient 的实例 final HttpClient httpClient = new HttpClient(manager); // 代理的主机 ProxyHost proxy = new ProxyHost("openproxy.huawei.com", 8080); // 使用代理 httpClient.getHostConfiguration().setProxyHost(proxy); // 创建Get连接方法的实例 final HttpMethod getMethod = new GetMethod(url); // 使用系统提供的默认的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); Thread t = new Thread(new Runnable() { @Override public void run() { try { // 请求URI System.out.println("executing request : " + url); // 执行getMethod int status = httpClient.executeMethod(getMethod); System.out.println("status:" + status); // 连接返回的状态码 if (HttpStatus.SC_OK == status) { System.out.println("Connection to " + getMethod.getURI() + " Success!"); // 获取到的内容 String responseBody = getMethod .getResponseBodyAsString(); System.out.println(responseBody); } } catch (URIException e) { e.printStackTrace(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }); // 将该线程标记为守护线程或用户线程 /* t.setDaemon(true); */ // 启动线程 t.start(); try { // 等待5s后结束 t.join(5000L); } catch (InterruptedException e) { t.interrupt(); e.printStackTrace(); } // 释放连接 getMethod.abort(); } } |
|||
HttpCLient4 | 通过httpClient4读取页面内容 | ||
HttpClient httpClient = new DefaultHttpClient(); HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY); HttpHost httpHost = new HttpHost("localhost"); HttpGet httpGet = new HttpGet("/https/"); HttpResponse response = httpClient.execute(httpHost,httpGet); if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){ //请求成功 //取得请求内容 HttpEntity entity = response.getEntity(); //显示内容 if (entity != null) { // 显示结果 System.out.println(EntityUtils.toString(entity,"utf-8")); } } httpGet.abort(); |
|||
HttpClient | HttpClient 代理实例(Get方式) 线程池 | ||
import java.io.IOException; import java.util.concurrent.*; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; /** * @ClassName: ThreadProxy * @Description: 根据输入的url,获取对应的内容 * @author * @date 2012-1-10 下午2:23:46 * @version V1.0 */ public class ThreadProxy { /** * @Title: main * @Description: 根据输入的url,获取对应的内容 * @param args * @author * @date 2012-1-10 */ public static void main(String[] args) { String url = "http://www.apache.org"; try { new ThreadProxy().get(url); } catch (Exception e) { e.printStackTrace(); } } /** * @Title: get * @Description: 根据输入的url,获取对应的内容 * @param url * @author * @date 2012-1-10 */ public void get(final String url) { MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager(); // 创建 HttpClient 的实例 final HttpClient httpClient = new HttpClient(manager); // 代理的主机 ProxyHost proxy = new ProxyHost("openproxy.huawei.com", 8080); // 使用代理 httpClient.getHostConfiguration().setProxyHost(proxy); // 创建Get连接方法的实例 final HttpMethod getMethod = new GetMethod(url); // 使用系统提供的默认的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // 创建缓冲线程池 ExecutorService service = Executors.newCachedThreadPool(); // Future 表示异步计算的结果 Future<String> future = service.submit(new Callable<String>() { @Override public String call() throws Exception { try { // 请求URI System.out.println("executing request : " + url); // 执行getMethod int status = httpClient.executeMethod(getMethod); System.out.println("status:" + status); // 连接返回的状态码 if (HttpStatus.SC_OK == status) { System.out.println("Connection to " + getMethod.getURI() + " Success!"); // 获取到的内容 String responseBody = getMethod .getResponseBodyAsString(); System.out.println(responseBody); } } catch (URIException e) { e.printStackTrace(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return ""; } }); try { //如有必要,最多等待为使计算完成所给定的时间之后,获取其结果(如果结果可用). future.get(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } service.shutdown(); // 释放连接 getMethod.abort(); } } |
|||
Https | 让httpclient接受所有ssl证书https 分享 | ||
import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.SocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory { static{ System.out.println(">>>>in MySecureProtocolSocketFactory>>"); } private SSLContext sslcontext = null; private SSLContext createSSLContext() { SSLContext sslcontext=null; try { sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return sslcontext; } private SSLContext getSSLContext() { if (this.sslcontext == null) { this.sslcontext = createSSLContext(); } return this.sslcontext; } public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket( socket, host, port, autoClose ); } public Socket createSocket(String host, int port) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket( host, port ); } public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort); } public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } } //自定义私有类 private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } } } |
|||
HttpClient download file | 使用httpclient下载文件 | ||
/** * 保存指定URL的源文件到指定路径下 * * @param srcUrl 要下载文件的绝对路径url * @param filePath 文件要保存的路径 */ public static synchronized void downloadFileByUrl(String srcUrl,String filePath) { System.out.print("下载"+srcUrl); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(srcUrl); HttpResponse response; FileOutputStream out = null; try { String[] array = srcUrl.split("\\/"); String[] fname = array[array.length-1].split("\\."); String fileName="",extname=""; if(fname.length == 2){ fileName = fname[0]; extname = fname[1]; } File wdFile = new File(filePath + fileName+"."+extname); //文件已存在 if(wdFile.exists()){ fileName += RandomID.GenTradeId(); wdFile = new File(filePath + fileName+"."+extname); } out = new FileOutputStream(wdFile); response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); int l; byte[] tmp = new byte[2048]; while ((l = instream.read(tmp)) != -1) { out.write(tmp, 0, l); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("............. 结束"); } |
|||
HttpClient | HttpClient实现HTTP文件通用下载类 | ||
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import org.apache.http.Header; import org.apache.http.HeaderElement; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; /** * 说明 * 利用httpclient下载文件 * maven依赖 * <dependency> * <groupId>org.apache.httpcomponents</groupId> * <artifactId>httpclient</artifactId> * <version>4.0.1</version> * </dependency> * 可下载http文件、图片、压缩文件 * bug:获取response header中Content-Disposition中filename中文乱码问题 * @author tanjundong * */ public class HttpDownload { public static final int cache = 10 * 1024; public static final boolean isWindows; public static final String splash; public static final String root; static { if (System.getProperty("os.name") != null && System.getProperty("os.name").toLowerCase().contains("windows")) { isWindows = true; splash = "\\"; root="D:"; } else { isWindows = false; splash = "/"; root="/search"; } } /** * 根据url下载文件,文件名从response header头中获取 * @param url * @return */ public static String download(String url) { return download(url, null); } /** * 根据url下载文件,保存到filepath中 * @param url * @param filepath * @return */ public static String download(String url, String filepath) { try { HttpClient client = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response = client.execute(httpget); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); if (filepath == null) filepath = getFilePath(response); File file = new File(filepath); file.getParentFile().mkdirs(); FileOutputStream fileout = new FileOutputStream(file); /** * 根据实际运行效果 设置缓冲区大小 */ byte[] buffer=new byte[cache]; int ch = 0; while ((ch = is.read(buffer)) != -1) { fileout.write(buffer,0,ch); } is.close(); fileout.flush(); fileout.close(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取response要下载的文件的默认路径 * @param response * @return */ public static String getFilePath(HttpResponse response) { String filepath = root + splash; String filename = getFileName(response); if (filename != null) { filepath += filename; } else { filepath += getRandomFileName(); } return filepath; } /** * 获取response header中Content-Disposition中的filename值 * @param response * @return */ public static String getFileName(HttpResponse response) { Header contentHeader = response.getFirstHeader("Content-Disposition"); String filename = null; if (contentHeader != null) { HeaderElement[] values = contentHeader.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("filename"); if (param != null) { try { //filename = new String(param.getValue().toString().getBytes(), "utf-8"); //filename=URLDecoder.decode(param.getValue(),"utf-8"); filename = param.getValue(); } catch (Exception e) { e.printStackTrace(); } } } } return filename; } /** * 获取随机文件名 * @return */ public static String getRandomFileName() { return String.valueOf(System.currentTimeMillis()); } public static void outHeaders(HttpResponse response) { Header[] headers = response.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } } public static void main(String[] args) { // String url = "http://bbs.btwuji.com/job.php?action=download&pid=tpc&tid=320678&aid=216617"; String url="http://www.dy1000.com/img/20120701/1999311085_150_200.JPG"; // String filepath = "D:\\test\\a.torrent"; String filepath = "D:\\test\\a.jpg"; HttpDownload.download(url, filepath); } } |