网络编程:多文件上传

本文详细介绍了一个使用Android系统进行文件上传的实例,包括开启子线程、设置HTTP请求参数、读取本地文件并将其发送到服务器的过程。通过示例代码,读者可以了解到如何构造multipart/form-data格式的数据包,以及如何处理上传后的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步骤

1.开启一个子线程
2.OutputStream outputStream=null;
3.写一个try
4.

try {
//手机上的文件
                            File file1=new File("/storage/emulated/0/Download/plus_logo_web.png");
                            File file2=new File("/storage/emulated/0/Download/plus_logo_web.png");
                            File file3=new File("/storage/emulated/0/Download/plus_logo_web.png");
                           //这里是文件特性
                            String fileKey="files";
                            String fileType="image/jpeg";
                            String BOUNDARY="--------------------------010040537206838198688029";
                            //这里是我们的服务器,根据服务器的来决定我们怎么使用
                            URL url=new URL("http://10.0.2.2:9102"+"/files/upload");
                            //下面是正常联网
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setRequestMethod("POST");
                            httpURLConnection.setConnectTimeout(10000);
                            httpURLConnection.setRequestProperty("User-Agent","Android/"+ Build.VERSION.SDK_INT);//客户端代理
                            httpURLConnection.setRequestProperty("Accept"," */*");//接收类型
                            httpURLConnection.setRequestProperty("Cache-Control","no-cache");
                            httpURLConnection.setRequestProperty("Content-Type","multipart/form-data; boundary="+BOUNDARY);//随机数
                            httpURLConnection.setRequestProperty("Connection","keep-alive");
                            httpURLConnection.setDoOutput(true);
                            httpURLConnection.setDoInput(true);
                            outputStream = httpURLConnection.getOutputStream();
                            //连接
                            httpURLConnection.connect();

//这里我们写一个方法,用于上传文件

 private void uploadFile(File file, String fileName, String fileKey, String fileType, String BOUNDARY, OutputStream outputStream,
                            boolean isLast) throws IOException {
    /*
    准备数据
    */
//                            头部数据(描述性文件)
        StringBuilder headerSbInfo=new StringBuilder();
        headerSbInfo.append("--");
        headerSbInfo.append(BOUNDARY);
        headerSbInfo.append("\r\n");//\r\n可以在window 和其他
        headerSbInfo.append("Content-Disposition: form-data; name=\""+fileKey+"\"; filename=\""+fileName+"\"");
        headerSbInfo.append("\r\n");
        headerSbInfo.append("Content-Type: "+fileType);
        headerSbInfo.append("\r\n");
        headerSbInfo.append("\r\n");
        byte[] headerInfoBytes = headerSbInfo.toString().getBytes("UTF-8");//转换为字符串,然后的到字节
        outputStream.write(headerInfoBytes);//写出去
//                            文件内容
        FileInputStream fos=new FileInputStream(file);
        BufferedInputStream bfi=new BufferedInputStream(fos);
        byte[] buffer=new byte[1024];
        int len ;
        while ((len=bfi.read(buffer,0,buffer.length))!=-1){
            outputStream.write(buffer,0,len);
        }
        //写尾部信息
        StringBuilder footerSBInfo=new StringBuilder();
        footerSBInfo.append("\r\n");
        footerSBInfo.append("--");
        footerSBInfo.append(BOUNDARY);
        if (isLast){
            footerSBInfo.append("--");
            footerSBInfo.append("\r\n");
        }
        footerSBInfo.append("\r\n");
        byte[] footerSBInfoBytes = footerSBInfo.toString().getBytes("UTF-8");
        outputStream.write(footerSBInfoBytes);
    }

5.这里是调用编写的方法,并获取返回结果

uploadFile(file1, file1.getName(), fileKey, fileType, BOUNDARY, outputStream,false);
                            uploadFile(file2, file2.getName(), fileKey, fileType, BOUNDARY, outputStream,false);
                            uploadFile(file3, file3.getName(), fileKey, fileType, BOUNDARY, outputStream,true);
                            outputStream.flush();
                            //获取返回结果
                            int responseCode=httpURLConnection.getResponseCode();
                            Log.d(TAG,"responseCode --> "+responseCode);
                            if (responseCode==HttpURLConnection.HTTP_OK){
                                InputStream inputStream = httpURLConnection.getInputStream();
                                BufferedReader bf=new BufferedReader(new InputStreamReader(inputStream));
                                String result=bf.readLine();
                                Log.d(TAG,"result -- >"+result);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            if (outputStream !=null){
                                try {
                                    outputStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值