• 用apache的httpclient发请求和接受数据


    此处发请求的是用httpclient4,请自己下载所需要的jar包。

    发post请求,并得到数据。

    String url = "http://localhost:8080/lee";
            url = url+ "/query/action/export.action";
            String exportFilePath = "lee"+".csv.";
            
            final HttpClient httpClient = new DefaultHttpClient();
            final HttpPost post = new HttpPost(url);
            List<NameValuePair> params = new ArrayList<NameValuePair>();  
            params.add(new BasicNameValuePair("leeSmart", leeSmart));//发post请求的参数
            post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
            final HttpResponse response = httpClient.execute(post);//得到返回的response
            final int code = response.getStatusLine().getStatusCode();
            final HttpEntity entity = response.getEntity();//得到entity
            if (entity != null && code < 400) {
                InputStream inputStream = entity.getContent();//得到从服务器端返回的数据流
                long length = entity.getContentLength();
                if(length<=0) return;
                int len = (int)length;
                byte[] b = new byte[len];
                int readCount = 0;
                        //建议用以下方式读inputStream为b赋值
                       while (readCount < len) {  
                	   readCount += inputStream.read(b, readCount, len - readCount);  
                } 
                //在客户端生成文件。更高效的做法是,在服务器端传过来一个压缩后的btye[],然后在客户端解压,减少传输数据。
                try {
                    FileOutputStream fo1 = null;
                    fo1 = new FileOutputStream(exportFilePath);
            	fo1.write(b);
                }
        		fo1.close();
        	      } catch (Exception e2) {
        		e2.printStackTrace();
        	     }
               }

    在action中接请求的方法export(),并返回数据流

    try {
    			request.setCharacterEncoding("UTF-8");
    		} catch (UnsupportedEncodingException e1) {
    			e1.printStackTrace();
    		}
    		response.setCharacterEncoding("UTF-8");
    		String leeSmart = request.getParameter("leeSmart");//前台传过来的post参数
    		byte[] b = null;
    		try{
                          List ret = serivce.query("select * from dual");//得到查询结果集
                          //将ret放到sb中
                          StringBuilder sb = new StringBuilder();
                          //.......对结果集进行处理,并转成字节数组
                          b = sb.toString().getByte();
                   }catch(Exception e){
                       e.printStackTrace();
                   }
                   //如果方便,可以把b字节数组压缩一下,这样传的数据会比较小一些。
                  //将字节数组放到response中,并返回到客户端。
                 try {
                       response.reset();
                      // 设置response的Header
                       response.addHeader("Content-Disposition", "attachment;filename=" + new String("random".getBytes("UTF-8"),"ISO-8859-1"));
                      response.addHeader("Content-Length", "" + b.length);
                      OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                      response.setContentType("application/octet-stream");
                      toClient.write(b);
                      toClient.flush();
                      toClient.close();
                 } catch (Exception e) {
                    e.printStackTrace();
                 }finally{
                
                 }
    
    
    
    
    
    

  • 相关阅读:
    java-数组
    编程练习
    java-循环语句
    java-条件语句if&switch
    JAVA-运算符
    JAVA-常量
    springAOP源码分析之篇一:配置文件的解析
    spring IOC容器的扩展
    spring IOC之篇六 bean的加载---bean的创建
    spring IOC之篇五 bean的加载--缓存中获取
  • 原文地址:https://www.cnblogs.com/riskyer/p/3293953.html
Copyright © 2020-2023  润新知