100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java 函数参数 返回值_java中如何用函数返回值作为post提交的参数?

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

时间:2024-02-26 09:46:11

相关推荐

java 函数参数 返回值_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 .HttpURLConnection;

import .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提交的参数,谢谢。

将返回数据组织好,无论以XML还是JSON 还是其它数据传输形式。

让页面用AJAX请求你的servlet,servlet里面就是你的方法,将返回值返回给调用的前端AJAX,前端拿到你的数据再做处理就可以了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。