java将本地图片上传到系统
时间: 2024-01-14 22:04:32 浏览: 35
中,需要使用java.io.File类和java.net.URL类来完成。具体步骤如下:
1. 创建一个File对象,指定本地图片所在的路径和文件名。
```
File file = new File("C:/pictures/test.jpg");
```
2. 创建一个URL对象,指定系统中存储图片的地址。
```
URL url = new URL("http://localhost:8080/upload");
```
3. 创建一个HttpURLConnection对象,用于上传图片。
```
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
```
4. 创建一个DataOutputStream对象,用于向服务器发送数据。
```
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
```
5. 使用FileInputStream读取本地图片文件,并将其写入DataOutputStream中。
```
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
```
6. 关闭DataOutputStream并获取服务器返回的结果。
```
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
```
完整代码示例:
```
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UploadImage {
public static void main(String[] args) {
try {
File file = new File("C:/pictures/test.jpg");
URL url = new URL("http://localhost:8080/upload");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
System.out.println("Server response code: " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
相关推荐
![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)
![](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)