java模拟表单上传文件
时间: 2023-11-04 07:49:20 浏览: 41
可以使用 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)来分隔不同的部分。这个分隔符需要随机生成,且不能与请求体中的其他内容重复。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)