php模拟form表单发送文件

php模拟form表单发送文件

1、背景

今天遇到一个需求,要在php本身上面发送文件数据,文件是客户端表单提交过来的,然后我要发给另一方,另一方也是php写的,但是它接收的是表单数据的文件上传:

2、解决方案:

(1)header设置:需要支持form表单提交

	$headers = [
    	'Content-Type:multipart/form-data',
    ];

(2)file文件设置:这里我直接使用客户端传给我的$_FILES变量,不做任何处理

$fileData = [
            // 使用CURLFile类 【参数是前端传输过来的】;
            'file' => new \CURLFile($_FILES["file"]['tmp_name'],$_FILES["file"]['type'],$_FILES["file"]['name']),
        ];

(3)发送数据

    public function postDataFileByCurl($postUrl, $postData, $header = array('Content-Type: application/json;charset=utf-8'))
    {
        $timeout = 100;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $postUrl);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        //设置header头信息
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        ob_start();
        $exe_result = curl_exec($ch);
        ob_end_clean();
        curl_close($ch);
        $exe_result = json_decode($exe_result,true);
        return $exe_result;
    }

具体调用按照顺序发过去就行了,这很初级,大家应该都会

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Java 的 HttpURLConnection 类来模拟表单上传文件。以下是一个简单的示例代码: ```java import java.io.*; import java.net.*; public class FileUploader { public static void main(String[] args) throws Exception { String url = "http://example.com/upload.php"; // 上传接口地址 String fileField = "file"; // 表单文件域的名称 String filePath = "/path/to/file.txt"; // 要上传的文件路径 File file = new File(filePath); URL uploadUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) uploadUrl.openConnection(); // 设置请求方法为 POST conn.setRequestMethod("POST"); conn.setDoOutput(true); // 设置请求头 String boundary = "---------------------------" + System.currentTimeMillis(); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); // 构造请求体 OutputStream out = conn.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8"), true); writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"" + fileField + "\"; filename=\"" + file.getName() + "\"").append("\r\n"); writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(file.getName())).append("\r\n"); writer.append("\r\n"); writer.flush(); FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); inputStream.close(); writer.append("\r\n").flush(); writer.append("--" + boundary + "--").append("\r\n"); writer.close(); // 发送请求并获取响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); conn.disconnect(); } } ``` 在上面的代码中,我们首先指定了上传接口的地址、表单文件域的名称和要上传的文件路径。然后构造了一个 URL 对象和 HttpURLConnection 对象,并设置了请求方法和请求头。接着构造了请求体,并将其写入输出流中。最后发送请求并获取响应。注意,在请求体中,我们使用了分隔符(boundary)来分隔不同的部分。这个分隔符需要随机生成,且不能与请求体中的其他内容重复。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值