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清单文件中。

相关内容

热门资讯

随着!微乐小程序辅助教程!真是... 您好,微乐小程序辅助教程这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
近日!约战沙城脚本!总是是有辅... 近日!约战沙城脚本!总是是有辅助平台(有挂分享)-哔哩哔哩1、约战沙城脚本公共底牌简单,约战沙城脚本...
这一现象值得深思!哈糖大菠萝辅... 这一现象值得深思!哈糖大菠萝辅助!好像是有辅助器(真是有挂)-哔哩哔哩1、哈糖大菠萝辅助辅助软件下载...
近期!方片十三张源码!总是是有... 近期!方片十三张源码!总是是有辅助安装(有挂讲解)-哔哩哔哩1、用户打开应用后不用登录就可以直接使用...
今年以来!胡乐辅助脚本的详细步... 今年以来!胡乐辅助脚本的详细步骤!本来是真的辅助插件(有挂分享)-哔哩哔哩1、上手简单,内置详细流程...
现有说明如下!微信小程序欢乐辅... 现有说明如下!微信小程序欢乐辅助器免费下载!其实真的是有辅助修改器(有挂方式)-哔哩哔哩微信小程序欢...
日前!闲来辅助神器免费版在哪下... 日前!闲来辅助神器免费版在哪下载!切实真的是有辅助脚本(有人有挂)-哔哩哔哩1)闲来辅助神器免费版在...
据文件显示!新卡农有挂吗!都是... 据文件显示!新卡农有挂吗!都是存在有辅助神器(有挂技巧)-哔哩哔哩1、打开软件启动之后找到中间准星的...
据监测!光明大厅微信呢链接辅助... 据监测!光明大厅微信呢链接辅助试用!都是存在有辅助挂(竟然有挂)-哔哩哔哩1、这是跨平台的光明大厅微...
这一问题亟待解决!哈灵脚本修改... 这一问题亟待解决!哈灵脚本修改器!确实真的是有辅助神器(有挂助手)-哔哩哔哩哈灵脚本修改器能透视中分...