帮我写一份java上传视频的代码
时间: 2023-03-29 11:00:19 浏览: 101
好的,以下是Java上传视频的代码示例:
```
import java.io.File;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
public class VideoUploader {
public static void main(String[] args) {
String url = "http://example.com/upload";
String filePath = "/path/to/video.mp4";
File videoFile = new File(filePath);
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
try {
Part[] parts = { new FilePart("video", videoFile) };
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
System.out.println("Video uploaded successfully!");
} else {
System.err.println("Video upload failed, status code: " + statusCode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
}
}
```
希望对您有所帮助!
阅读全文