java - Java中PHP的CURLOPT_POSTFIELDS相当于什么

标签 java http-post

我正在尝试为 putlokcer 网站编写一个 uploader 类,他们有 API 支持。

根据他们的文档,他们给出了一个上传示例如下

// Variables to Post
$local_file = "/path/to/filename.avi"; 
$file_to_upload = array(
    'file'=>'@'.$local_file, 
    'convert'=>'1', //optional Argument
    'user'=>'YOUR_USERNAME', 
    'password'=>'YOUR_PASSWORD', 
    'folder'=>'FOLDER_NAME'   // Optional Argument
); 

// Do Curl Request
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'http://upload.putlocker.com/uploadapi.php'); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch); 
curl_close ($ch); 

// Do Stuff with Results
echo $result; 

现在我正尝试使用 java.net 包按照 Java 进行转换,如下所示,

BufferedInputStream bis = null; BufferedOutputStream bos = null; 尝试 { 网址 url = 新网址("http://upload.putlocker.com/uploadapi.php"); URLConnection uc = url.openConnection(); uc.setDoOutput(真); uc.setDoInput(真); uc.setAllowUserInteraction(false);

        bos = new BufferedOutputStream(uc.getOutputStream());
        bis = new BufferedInputStream(new FileInputStream("C:\\Dinesh\\Naruto.jpg"));

        int i;

        bos.write("file".getBytes());

        // read byte by byte until end of stream
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }

        bos.write("user=myusername".getBytes());
        bos.write("password=mypassword".getBytes());
        br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String k = "",tmp="";
        while ((tmp = br.readLine()) != null) {
            System.out.println(tmp);
            k += tmp;
        }
    } catch (Exception e) {
        System.out.println(e);
    }

我收到的响应是“没有传递有效的登录名或文件”,这意味着我的 HTTP POST 请求没有发送有效的邮寄文件。

任何人都可以向我解释如何使用 java.net 包进行这项工作吗?

最佳答案

在为表单发布构建请求正文时,您必须遵循特定的格式,当您在 PHP 中使用 curl 时,您会发现很多复杂性。我建议您查看类似 Apache HTTPComponents 客户端的内容,如 this question 的答案中所述。而不是尝试自己动手。

(尽管如果您确实想手工完成,详情请参见 in this answer)

关于java - Java中PHP的CURLOPT_POSTFIELDS相当于什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12541790/

相关文章:

java - 初始化期间分配 null 的字符串数组中的空指针异常

Java 枚举查找枚举

javascript - JxBrowser 在自定义 ProtocolHandler 中获取帖子正文

jquery - 发送 ajax 调用时出错 [内部服务器错误 500]

c# - 取消一个 'HttpClient' POST 请求

php - IIS7 + PHP + HTTP POST = 挂起?

java - 运行时 JDBC 程序中的 MySQLNonTransientConnectionException

java - OneToMany 注释列表中的重复项

java - 使用哈希表创建单词字典

javascript - 如何将多个 map 对象(JSON)添加到cloud-firestore数据库?