java 用 post multipart/form-data

httpURLConnection.setRequestProperty("Content-type",   "multipart/form-data;   boundary=---------------------------7d318fd100112"); 
httpURLConnection.setRequestProperty("Connection",   "Keep-Alive");
httpURLConnection.setRequestProperty("Cache-Control",   "no-cache");
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
DataOutputStream out  = new DataOutputStream(httpURLConnection.getOutputStream());
out.writeBytes("-----------------------------7d318fd100112/r/n");
for(KeyValue v:post){
    if(v.isFile()){
        try{  
           out.writeBytes("content-disposition:   form-data;   name=/""+v.getKey()+"/";   filename=/""+v.getValue()+"/"/r/n");  
           out.writeBytes("content-type:   application/octet-stream/r/n");  
           FileInputStream   fis   =   new   FileInputStream(v.getValue());  
           byte[] buffer = new byte[1024];
           while(true){  
           synchronized(buffer){  
               int  amountRead = fis.read(buffer);  
                if(amountRead==-1){  
                      break;  
                }
                out.write(buffer,0,amountRead);  
            }
            }
            fis.close();
         }catch(IOException e){
              log.error(e.getMessage());
         }   
     }else{
         out.writeBytes("content-disposition:   form-data;   name=/""   +   v.getKey()+   "/"/r/n/r/n");  
          out.writeBytes(v.getValue());  
                  
     }
     out.writeBytes("/r/n-----------------------------7d318fd100112/r/n");
}
out.flush();
out.close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中发送multipart/form-data类型的POST请求,可以通过使用Java的HttpURLConnection类或Apache的HttpClient库来实现。 使用HttpURLConnection类的示例代码如下: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class MultipartFormDataExample { public static void main(String[] args) { try { // 创建URL对象 URL url = new URL("http://example.com/upload"); // 打开连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); // 设置Content-Type为multipart/form-data connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); // 允许输入输出流 connection.setDoInput(true); connection.setDoOutput(true); // 创建请求体 String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; String CRLF = "\r\n"; // 回车换行符 // 设置请求参数 String data = "--" + boundary + CRLF + "Content-Disposition: form-data; name=\"file\"; filename=\"example.txt\"" + CRLF + "Content-Type: text/plain" + CRLF + CRLF + "This is the content of the file" + CRLF + "--" + boundary + "--" + CRLF; // 获取输出流 OutputStream outputStream = connection.getOutputStream(); // 写入请求体 outputStream.write(data.getBytes()); // 关闭输出流 outputStream.close(); // 获取响应状态码 int responseCode = connection.getResponseCode(); // 处理响应结果 if (responseCode == HttpURLConnection.HTTP_OK) { // 获取响应输入流 InputStream inputStream = connection.getInputStream(); // 读取响应内容 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder response = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { response.append(line); } // 关闭输入流 inputStream.close(); // 打印响应内容 System.out.println(response.toString()); } else { System.out.println("POST request failed with response code: " + responseCode); } // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意,在代码中需要替换URL、boundary、filename和请求体中的内容为实际的值。 Apache HttpClient库也提供了发送multipart/form-data类型请求的功能,使用方法请参考相关文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值