我正在尝试为 putlokcer 网站编写一个上传器类,他们有 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 url = new URL("http://upload.putlocker.com/uploadapi.php"); URLConnection uc = url.openConnection(); uc.setDoOutput(true); uc.setDoInput(true); 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 包完成这项工作?