网络文件下载与上传

要下载网络文件,首先需要获取网络文件的URL。可以使用Java中的URL类来创建URL对象,并通过openStream方法获取网络文件的输入流。然后,我们可以使用Java File中的FileOutputStream类将网络文件的输入流写入到本地文件中,实现文件的下载。

下面是一个简单的示例代码,用于下载网络文件:

 1 import java.io.FileOutputStream;
 2 import java.io.IOException;
 3 import java.io.InputStream;
 4 import java.net.URL;
 5 
 6 public class FileDownloader {
 7     public static void downloadFile(String fileUrl, String savePath) throws IOException {
 8         URL url = new URL(fileUrl);
 9         try (InputStream inputStream = url.openStream();
10              FileOutputStream outputStream = new FileOutputStream(savePath)) {
11             byte[] buffer = new byte[4096];
12             int bytesRead;
13             while ((bytesRead = inputStream.read(buffer)) != -1) {
14                 outputStream.write(buffer, 0, bytesRead);
15             }
16         }
17     }
18 
19     public static void main(String[] args) {
20         String fileUrl = "
21         String savePath = "C:/path/to/save/example.txt";
22         try {
23             downloadFile(fileUrl, savePath);
24             System.out.println("File downloaded successfully.");
25         } catch (IOException e) {
26             e.printStackTrace();
27         }
28     }
29 }

2.2 上传文件到服务器
要上传文件到服务器,首先需要获取服务器的URL和文件的本地路径。然后,我们可以使用Java File中的FileInputStream类将本地文件的输入流读取出来,并通过HTTP请求将文件发送到服务器。

下面是一个简单的示例代码,用于上传文件到服务器:

 

 1 import java.io.*;
 2 import java.net.HttpURLConnection;
 3 import java.net.URL;
 4 
 5 public class FileUploader {
 6     public static void uploadFile(String fileUrl, String serverUrl) throws IOException {
 7         URL url = new URL(serverUrl);
 8         File file = new File(fileUrl);
 9         try (InputStream inputStream = new FileInputStream(file);
10              OutputStream outputStream = url.openConnection().getOutputStream()) {
11             byte[] buffer = new byte[4096];
12             int bytesRead;
13             while ((bytesRead = inputStream.read(buffer)) != -1) {
14                 outputStream.write(buffer, 0, bytesRead);
15             }
16         }
17     }
18 
19     public static void main(String[] args) {
20         String fileUrl = "C:/path/to/upload/example.txt";
21         String serverUrl = "
22         try {
23             uploadFile(fileUrl, serverUrl);
24             System.out.println("File uploaded successfully.");
25         } catch (IOException e) {
26             e.printStackTrace();
27         }
28     }
29 }

2.3 删除网络文件
要删除网络文件,我们可以使用Java中的URL类来创建URL对象,并通过URLConnection类的实例来发送HTTP DELETE请求,将要删除的文件的URL作为请求的目标。

下面是一个简单的示例代码,用于删除网络文件:

 

 1 import java.io.IOException;
 2 import java.net.HttpURLConnection;
 3 import java.net.URL;
 4 
 5 public class FileDeleter {
 6     public static void deleteFile(String fileUrl) throws IOException {
 7         URL url = new URL(fileUrl);
 8         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 9         connection.setRequestMethod("DELETE");
10         int responseCode = connection.getResponseCode();
11         if (responseCode == HttpURLConnection.HTTP_OK) {
12             System.out.println("File deleted successfully.");
13         } else {
14             System.out.println("Failed to delete file. Response code: " + responseCode);
15         }
16     }
17 
18     public static void main(String[] args) {
19         String fileUrl = "
20         try {
21             deleteFile(fileUrl);
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25     }
26 }

 

posted @ 2024-01-31 14:44  liyongliang的博客  阅读(142)  评论(0)    收藏  举报