http post上传数据

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONObject;

public class HttpUtils {
        public static void appadd(String ADD_URL,JSONObject josnObj) {
            try {
                URL url = new URL(ADD_URL);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
                connection.setRequestProperty("Accept-Charset", "utf-8");
                connection.setRequestProperty("contentType", "utf-8");
                connection.connect();
                //POST请求
                DataOutputStream out = new DataOutputStream(
                        connection.getOutputStream());
                out.write(josnObj.toString().getBytes("UTF-8"));
                System.out.println("data=="+josnObj.toString());
                out.flush();
                out.close();
                //读取响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sb.append(lines);
                }
                System.out.println(sb.toString());
                reader.close();
                connection.disconnect();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }       
}

测试代码

    import net.sf.json.JSONObject;

    public class Test(){
            public static void main(String[] args) {
            JSONObject josnObj = new JSONObject();
            josnObj.put("type", "bb");
            josnObj.put("name", "aa");
            HttpUtils.appadd("http://127.0.0.1:8080/test", josnObj);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用HttpPost实现数据流方式上传图片的步骤如下: 1. 创建HttpClient对象 ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 2. 创建HttpPost对象,并设置请求URL ```java HttpPost httpPost = new HttpPost("http://example.com/upload"); ``` 3. 创建HttpEntity对象,并设置上传的文件和参数 ```java MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // 设置文件参数 File file = new File("image.jpg"); entityBuilder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName()); // 设置其他参数 entityBuilder.addTextBody("name", "example"); HttpEntity httpEntity = entityBuilder.build(); httpPost.setEntity(httpEntity); ``` 4. 执行HttpPost请求,并获取响应结果 ```java CloseableHttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); String response = EntityUtils.toString(responseEntity); ``` 完整的代码示例: ```java import java.io.File; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class ImageUploader { public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://example.com/upload"); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // 设置文件参数 File file = new File("image.jpg"); entityBuilder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName()); // 设置其他参数 entityBuilder.addTextBody("name", "example"); HttpEntity httpEntity = entityBuilder.build(); httpPost.setEntity(httpEntity); CloseableHttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); String response = EntityUtils.toString(responseEntity); System.out.println(response); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值