Android - 如何在服务器和客户端之间传输文件
创始人
2024-09-30 09:30:15
0

在Android中,可以使用HTTP协议来在服务器和客户端之间传输文件。以下是一个示例代码,演示了如何在Android中上传和下载文件:

  1. 上传文件到服务器:
public class FileUploadTask extends AsyncTask {

    @Override
    protected Void doInBackground(String... params) {
        String filePath = params[0];
        String uploadUrl = params[1];

        try {
            File file = new File(filePath);

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(uploadUrl);

            FileBody fileBody = new FileBody(file);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            builder.addPart("file", fileBody);

            httpPost.setEntity(builder.build());

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {
                String responseStr = EntityUtils.toString(resEntity).trim();
                Log.d("FileUpload", "Response: " + responseStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // 上传完成后的操作
    }
}

使用示例:

String filePath = "/path/to/file";
String uploadUrl = "http://example.com/upload";

FileUploadTask fileUploadTask = new FileUploadTask();
fileUploadTask.execute(filePath, uploadUrl);
  1. 从服务器下载文件:
public class FileDownloadTask extends AsyncTask {

    @Override
    protected Void doInBackground(String... params) {
        String downloadUrl = params[0];
        String saveFilePath = params[1];

        try {
            URL url = new URL(downloadUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.connect();

            InputStream inputStream = connection.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(saveFilePath);

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

            fileOutputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // 下载完成后的操作
    }
}

使用示例:

String downloadUrl = "http://example.com/file.txt";
String saveFilePath = "/path/to/save/file.txt";

FileDownloadTask fileDownloadTask = new FileDownloadTask();
fileDownloadTask.execute(downloadUrl, saveFilePath);

注意:在使用这些代码示例之前,请确保已经添加必要的网络权限到你的Android清单文件中。

相关内容

热门资讯

黑科技辅助挂(德州免费)wPk... 德州新手教程相关信息汇总(需添加指定威信136704302获取下载链接);黑科技辅助挂(德州免费)w...
黑科技脚本(Wepoke总结)... 黑科技脚本(Wepoke总结)轰趴十三水原本是真的有挂!太坑了果然真的是有挂(2023已更新)(哔哩...
黑科技攻略(微扑克模拟器)wp... 黑科技攻略(微扑克模拟器)wpk游戏原先是有挂!太嚣张了确实存在有挂(2025已更新)(哔哩哔哩);...
黑科技有挂(哈糖大菠萝洗牌)w... 黑科技有挂(哈糖大菠萝洗牌)wPk好像存在有挂!太嚣张了一直有挂(2026已更新)(哔哩哔哩);实战...
黑科技脚本(微扑克ai)Wep... 黑科技脚本(微扑克ai)WepokE确实存在有挂!太嚣张了本然真的是有挂(2020已更新)(哔哩哔哩...
黑科技私人局(aapoker辅... 黑科技私人局(aapoker辅助)AAPoKER真是是有挂!太嚣张了原来是有挂(2025已更新)(哔...
黑科技苹果版(Wepoke助手... 黑科技苹果版(Wepoke助手)wPK原生存在有挂!太实锤了竟然存在有挂(2024已更新)(哔哩哔哩...
黑科技代打(德扑之星模拟器)a... 黑科技代打(德扑之星模拟器)aapokEr固有真的是有挂!太夸张了一直是有挂(2025已更新)(哔哩...
辅助黑科技(aapoker教程... 辅助黑科技(aapoker教程)epoker先前是真的有挂!太夸张了起初真的有挂(2022已更新)(...
黑科技存在(微扑克ai)Wep... 黑科技存在(微扑克ai)WepokE确实存在有挂!太嚣张了总是真的是有挂(2023已更新)(哔哩哔哩...