為父母生,為老婆死,為程序奮斗一輩子,吃眼前虧,上公司的當,最后死在客戶的需求上

          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 赫赫 閱讀(17446) | 評論 (1)編輯 收藏

               摘要:     HttpClient 是我最近想研究的東西,以前想過的一些應用沒能有很好的實現,發現這個開源項目之后就有點眉目了,令人頭痛的cookie問題還是有辦法解決滴。在網上整理了一些東西,寫得很好,寄放在這里。    HTTP 協議可能是現在 Internet 上使用得最多、最重要的協議了,越來越多的 Java 應用程序需要直接通過 HT...  閱讀全文

          posted @ 2012-10-23 07:20 赫赫 閱讀(327) | 評論 (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 赫赫 閱讀(286) | 評論 (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 赫赫 閱讀(448) | 評論 (0)編輯 收藏

          用兩年的工作經驗,從零開始,重新開始做一個稱職的程序員!
          主站蜘蛛池模板: 分宜县| 修水县| 东阿县| 荥阳市| 库伦旗| 景谷| 兴山县| 左权县| 天峻县| 和林格尔县| 湟中县| 喀喇沁旗| 塔城市| 达孜县| 石渠县| 香格里拉县| 灵武市| 贡觉县| 南康市| 汾阳市| 伽师县| 章丘市| 广灵县| 开远市| 庆云县| 五原县| 桑植县| 朝阳区| 聊城市| 平凉市| 沾化县| 深水埗区| 平江县| 龙里县| 杭锦后旗| 伊春市| 漯河市| 都匀市| 大渡口区| 探索| 乐都县|