上传文件的同时还需要附带其它参数
时间: 2023-09-19 19:09:09 浏览: 46
好的,如果你需要在上传文件的同时还需要附带其它参数,可以按照以下方式修改示例代码:
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileUploader {
public static void main(String[] args) {
String url = "http://example.com/upload";
String filePath = "/path/to/file.jpg";
String fileName = "file.jpg";
String paramName = "param1";
String paramValue = "value1";
try {
URL uploadUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) uploadUrl.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
OutputStream outputStream = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
// 写入文件参数
writer.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n");
writer.append("Content-Type: " + HttpURLConnection.guessContentTypeFromName(fileName) + "\r\n\r\n");
writer.flush();
FileInputStream fileInputStream = new FileInputStream(filePath);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
fileInputStream.close();
writer.append("\r\n");
// 写入其它参数
writer.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
writer.append("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n\r\n");
writer.append(paramValue + "\r\n");
writer.flush();
writer.append("------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n");
writer.flush();
writer.close();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println(response.toString());
} else {
System.out.println("Error uploading file. Response code: " + responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的示例代码中,我们新增了一个参数名为`param1`,值为`value1`的参数。在写入文件参数后,我们通过`writer.append()`方法写入参数名和参数值即可。同时,我们也需要在结束时写入一个分隔符`------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n`来告诉服务器参数已经结束。你可以根据实际情况修改示例代码中的参数名和参数值。
相关推荐
![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_column_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)
![](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)