/ISAPI/SDT/pictureUpload java示例
时间: 2023-06-30 16:20:09 浏览: 50
以下是一个简单的 Java 示例代码,用于上传图片到海康威视的设备中:
```java
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
public class PictureUploadDemo {
public static void main(String[] args) {
String ip = "192.168.1.100"; // 设备的 IP 地址
String username = "admin"; // 登录用户名
String password = "123456"; // 登录密码
String picturePath = "C:\\pictures\\test.jpg"; // 待上传的图片路径
String requestUrl = "http://" + ip + "/ISAPI/SDT/pictureUpload"; // 图片上传接口地址
try {
// 登录获取 cookie,用于后续请求的认证
String cookie = login(ip, username, password);
// 上传图片
String result = uploadPicture(requestUrl, picturePath, cookie);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 登录设备,获取 cookie
*/
public static String login(String ip, String username, String password) throws IOException {
String requestUrl = "http://" + ip + "/ISAPI/Security/userCheck";
HttpURLConnection conn = (HttpURLConnection) new URL(requestUrl).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/xml");
// 构造登录请求的 XML 数据
String xmlData = String.format("<UserCheck><userName>%s</userName><password>%s</password></UserCheck>", username, password);
// 发送登录请求
OutputStream out = conn.getOutputStream();
out.write(xmlData.getBytes());
out.flush();
out.close();
// 获取 cookie
String cookie = conn.getHeaderField("Set-Cookie");
conn.disconnect();
return cookie;
}
/**
* 上传图片
*/
public static String uploadPicture(String requestUrl, String picturePath, String cookie) throws IOException {
HttpURLConnection conn = (HttpURLConnection) new URL(requestUrl).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + UUID.randomUUID().toString());
conn.setRequestProperty("Cookie", cookie);
// 构造 multipart/form-data 请求体
OutputStream out = conn.getOutputStream();
PrintWriter writer = new PrintWriter(out);
// 第一个部分,图片文件
File pictureFile = new File(picturePath);
writer.printf("--%s\r\n", UUID.randomUUID().toString());
writer.printf("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n", pictureFile.getName());
writer.printf("Content-Type: image/jpeg\r\n\r\n");
writer.flush();
InputStream pictureIn = PictureUploadDemo.class.getResourceAsStream(picturePath);
byte[] buffer = new byte[1024];
int len;
while ((len = pictureIn.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
pictureIn.close();
writer.printf("\r\n--%s--\r\n", UUID.randomUUID().toString());
writer.flush();
// 发送请求并获取响应
out.flush();
out.close();
int responseCode = conn.getResponseCode();
InputStream in;
if (responseCode >= 200 && responseCode < 300) {
in = conn.getInputStream();
} else {
in = conn.getErrorStream();
}
buffer = new byte[1024];
StringBuilder response = new StringBuilder();
while ((len = in.read(buffer)) != -1) {
response.append(new String(buffer, 0, len));
}
in.close();
conn.disconnect();
return response.toString();
}
}
```
在这个示例代码中,我们通过 `login` 方法登录设备,并获取到了认证用的 cookie。然后,在 `uploadPicture` 方法中,我们构造了一个 `multipart/form-data` 的 POST 请求,将待上传的图片文件作为其中的一个部分。最后,我们发送请求并获取响应,将响应的内容作为方法的返回值。
注意,这个代码中的 `picturePath` 是指图片文件在本地的路径,如果你要上传的图片文件不在本地文件系统中,你需要对代码进行相应的修改。