开发者社区> 问答> 正文

java中如何用函数返回值作为post提交的参数?

1.我想实现的功能是在java程序中导入HttpURLConnection类,然后将函数的值作为post方法要提交的参数,最后显示在显示台上。

2.要用到的函数是自己写的可以显示实时计算机cpu、内存、硬盘利用率的一个方法,返回值是String.

3.这是调用HttpURLConnection的代码

package com.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import agent.WindowsInfoUtil;

public class HttpURLConnectionTest {
    
    //发送一个POST请求,参数形式key1=value1&key2=value2...
    public static String post(String path,String params) throws Exception{
        HttpURLConnection httpConn=null;
        BufferedReader in=null;
        PrintWriter out=null;
        try {
            URL url=new URL(path);
            httpConn=(HttpURLConnection)url.openConnection();
            httpConn.setRequestMethod("POST");
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
             
            //发送post请求参数
            out=new PrintWriter(httpConn.getOutputStream());
            out.println(params);
            out.flush();
             
            //读取响应
            if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
                StringBuffer content=new StringBuffer();
                String tempStr="";
                in=new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
                while((tempStr=in.readLine())!=null){
                    content.append(tempStr);
                }
                return content.toString();
            }else{
                throw new Exception("请求出现了问题!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            in.close();
            out.close();
            httpConn.disconnect();
        }
        return null;
    }
     
    public static void main(String[] args) throws Exception {
        
        
        String resMessage=HttpURLConnectionTest.post("http://localhost:8888/test.jsp", "CPU=20&MEMORY=40&DISK=50");//想实现将函数返回值作为参数但是不知道该怎么弄?
        System.out.println(resMessage);
    }
 
}

4.另一个显示cpu、内存、硬盘利用率的函数如下:

 package agent;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.io.PrintStream;
    import java.lang.management.ManagementFactory;
    import java.util.ArrayList;
    import java.util.List;
    import com.sun.management.OperatingSystemMXBean;
     
    public class WindowsInfoUtil {
        private static final int CPUTIME = 500;
        private static final int PERCENT = 100;
        private static final int FAULTLENGTH = 10;
        public static void main(String[] args) throws FileNotFoundException {
            System.out.println(getCpuRatioForWindows());
            System.out.println(getMemery());
            System.out.println(getDisk());
        }
     // 获取内存使用率
    public static String getMemery() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
                .getOperatingSystemMXBean();
        // 总的物理内存+虚拟内存
        long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
        // 剩余的物理内存
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
        Double compare = (Double) (1 - freePhysicalMemorySize * 1.0
                / totalvirtualMemory) * 100;
        String str = "内存已使用:" + compare.intValue() + "%";
        return str;
         }
     // 获取文件系统使用率
     public static List<String> getDisk() {
         // 操作系统
         List<String> list = new ArrayList<String>();
         for (char c = 'A'; c <= 'Z'; c++) {
             String dirName = c + ":/";
             File win = new File(dirName);
             if (win.exists()) {
                 long total = (long) win.getTotalSpace();
                 long free = (long) win.getFreeSpace();
                 Double compare = (Double) (1 - free * 1.0 / total) * 100;
                 String str = c + ":盘  已使用 " + compare.intValue() + "%";
                 list.add(str);
             }
         }
      return list;
     }
     // 获得cpu使用率
     public static String getCpuRatioForWindows() {
        //...
         }
    }
        

5.我想知道如何能够将函数的返回值作为post提交的参数,谢谢。

展开
收起
小旋风柴进 2016-03-04 13:11:46 2867 0
1 条回答
写回答
取消 提交回答
  • 将返回数据组织好,无论以XML还是JSON 还是其它数据传输形式。
    让页面用AJAX请求你的servlet,servlet里面就是你的方法,将返回值返回给调用的前端AJAX,前端拿到你的数据再做处理就可以了。

    2019-07-17 18:52:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载