为父母生,为老婆死,为程序奋斗一辈子,吃眼前亏,上公司的当,最后死在客户的需求上

Hector_封嘴

华子说:看破红尘,与程序为伴!
posts - 4, comments - 1, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2012年10月23日

     摘要: 第一种方法,适用于httpclient4.X 里边有get和post两种方法供你发送请求使用。导入证书发送请求的在这里就不说了,网上到处都是 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->import java.io.BufferedR...  阅读全文

posted @ 2012-10-23 07:26 赫赫 阅读(17359) | 评论 (1)编辑 收藏

     摘要:     HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的实现,发现这个开源项目之后就有点眉目了,令人头痛的cookie问题还是有办法解决滴。在网上整理了一些东西,写得很好,寄放在这里。    HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HT...  阅读全文

posted @ 2012-10-23 07:20 赫赫 阅读(317) | 评论 (0)编辑 收藏

文件下载采用多线程方式能够充分利用CPU资源,关键点是设置线程的读取开始和结束位置。下面的代码,采用线程池启动10个线程来执行下载
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class FileDownLoadTest {
    
    
    
private static final int TCOUNT = 10;
    
    
private CountDownLatch latch = new CountDownLatch(TCOUNT);

    
private long completeLength = 0;
    
    
private long fileLength;
    
/**
     * 
@param args
     * 
@throws Exception 
     
*/

    
public static void main(String[] args) throws Exception {
        
        
new FileDownLoadTest().download("http://localhost:8080/test/IESSAction.docx");
    }

    
    
    
public void download(String address) throws Exception{
        ExecutorService service 
= Executors.newFixedThreadPool(TCOUNT);
        URL url 
= new URL(address);
        URLConnection cn 
= url.openConnection();
        cn.setRequestProperty(
"Referer""http://www.test.com");
        fileLength 
= cn.getContentLength();
        
long packageLength = fileLength/TCOUNT;
        
long leftLength = fileLength%TCOUNT;
        RandomAccessFile file 
= new RandomAccessFile("d:\\test.docx","rw");
        
//计算每个线程请求文件的开始和结束位置
        long pos = 0;
        
long endPos = pos + packageLength;
        
for(int i=0; i<TCOUNT; i++){
            
if(leftLength >0){
                endPos 
++;
                leftLength
--;
            }

            service.execute(
new DownLoadThread(url, file, pos, endPos));
            pos 
= endPos;
            endPos 
= pos + packageLength;
        }

        System.out.println(
"waiting.");
        
long begin = System.currentTimeMillis();
        latch.await();
        file.close();
        System.out.println(
"end.");
        System.out.println(System.currentTimeMillis() 
- begin + "ms");
        service.shutdown();
    }

    
    
class DownLoadThread implements Runnable{
        
        
private URL url;
        
private RandomAccessFile file;
        
private long from;
        
private long end;
        
        DownLoadThread(URL url, RandomAccessFile file, 
long from, long end){
            
this.url = url;
            
this.file = file;
            
this.from = from;
            
this.end = end;
        }

        
        
        
public void run() {
            
long pos = from;
            
byte[] buf = new byte[512];
            
try {
                HttpURLConnection cn 
= (HttpURLConnection) url.openConnection();
                cn.setRequestProperty(
"Range""bytes=" + from + "-" + end);
                
if(cn.getResponseCode() != 200 && cn.getResponseCode()!=206){
                    run();
                    
return;
                }

                BufferedInputStream bis 
= new BufferedInputStream(cn.getInputStream());
                
int len ;
                
while((len = bis.read(buf)) != -1){
//                    synchronized(file){
                        file.seek(pos);
                        file.write(buf, 
0, len);
//                    }
                    pos += len;
                    completeLength 
+=len;
                    System.out.println(
"threadName: " + Thread.currentThread().getName() 
                            
+ "persent: " + completeLength * 100 /fileLength + "%");
                }

                cn.disconnect();
                latch.countDown();
            }
 catch (IOException e) {
                e.printStackTrace();
                
            }

        }

    }

}

posted @ 2012-10-23 06:52 赫赫 阅读(275) | 评论 (0)编辑 收藏

2012年10月22日

     摘要: 做一个导出Excel的功能,项目前台用的ExtJS,后台用的JAVA,网上查了查,大概有3种做法。 1.apache公司提供的POI 2.韩国公司的JXL 3.据说是官方提供的JS调用方法 前两种都要引入外包,懒得找包了,采用了第三种,所需引入JS代码如下: Code highlighting produced by Actipro CodeHighlighter (freewa...  阅读全文

posted @ 2012-10-22 16:15 赫赫 阅读(437) | 评论 (0)编辑 收藏

用两年的工作经验,从零开始,重新开始做一个称职的程序员!