HttpURLConnection Post请求上传文件和参数到servlet

前台代码:

public String uplaod(String actionUrl, Map<String, String> params) {
        InputStream in = null;
        String BOUNDARY = java.util.UUID.randomUUID().toString();
        String PREFFIX = "--", LINEND = "\r\n";
        String MULTIPART_FROM_DATA = "multipart/form-data";
        String CHARSET = "UTF-8";
        URL uri;
        StringBuilder sb2 = null;
        String filePath = params.get("FILE_PATH");
        try {
            uri = new URL(actionUrl);
            HttpURLConnection conn = (HttpURLConnection) uri.openConnection();// 设置从主机读取数据超时
            conn.setReadTimeout(10 * 1000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

            // 首先组拼文本类型的参数
            StringBuilder sb = new StringBuilder();
   

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Servlet中发送POST请求可以使用以下步骤: 1. 创建一个URL对象,指定请求的目标地址。 ``` URL url = new URL("http://example.com/path/to/target"); ``` 2. 打开URL连接并设置请求方法为POST。 ``` HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); ``` 3. 设置请求头信息(可选)。 ``` connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); ``` 4. 设置请求体内容(如果有)。 ``` String requestBody = "param1=value1&param2=value2"; connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(requestBody.getBytes()); outputStream.flush(); outputStream.close(); ``` 5. 获取响应结果。 ``` int responseCode = connection.getResponseCode(); String responseBody = ""; if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { responseBody += line; } reader.close(); inputStream.close(); } ``` 完整的示例代码如下: ``` protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String targetUrl = "http://example.com/path/to/target"; String requestBody = "param1=value1&param2=value2"; URL url = new URL(targetUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(requestBody.getBytes()); outputStream.flush(); outputStream.close(); int responseCode = connection.getResponseCode(); String responseBody = ""; if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { responseBody += line; } reader.close(); inputStream.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值