中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

java如何上傳文件到指定服務器

小億
214
2024-03-22 19:05:10
欄目: 云計算

在 Java 中可以使用 Apache HttpClient 或者 HttpURLConnection 來實現文件上傳到指定服務器,以下是兩種方法的示例:

使用 Apache HttpClient:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;

import java.io.File;
import java.io.IOException;

public class FileUploader {

    public static void main(String[] args) {
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://example.com/upload");

        File file = new File("path/to/file.txt");

        HttpEntity entity = MultipartEntityBuilder.create()
                .addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName())
                .build();

        httpPost.setEntity(entity);

        try {
            HttpResponse response = httpClient.execute(httpPost);
            System.out.println(response.getStatusLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用 HttpURLConnection:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileUploader {

    public static void main(String[] args) {
        String url = "http://example.com/upload";
        String fileName = "path/to/file.txt";

        try {
            URL serverUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) serverUrl.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "multipart/form-data");

            File file = new File(fileName);
            FileInputStream fileInputStream = new FileInputStream(file);
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();

            int responseCode = connection.getResponseCode();
            System.out.println("Server response code: " + responseCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上示例代碼演示了如何使用 Apache HttpClient 和 HttpURLConnection 來上傳文件到指定服務器。在實際應用中,需要根據服務器的接口要求和認證方式進行適當的配置。

0
元谋县| 泰和县| 广饶县| 江西省| 富顺县| 双流县| 南江县| 灵武市| 阿克苏市| 缙云县| 哈尔滨市| 石渠县| 长岭县| 本溪市| 乌鲁木齐县| 凤凰县| 报价| 临颍县| 库尔勒市| 砀山县| 崇左市| 尖扎县| 乡宁县| 新昌县| 韶关市| 仁怀市| 赫章县| 阿拉善盟| 喀喇沁旗| 边坝县| 酒泉市| 林芝县| 南川市| 濮阳县| 东乡县| 榆林市| 荆门市| 禹城市| 德兴市| 五原县| 张掖市|