dongyou4411 2016-04-29 07:32
浏览 73
已采纳

在Android应用程序和php服务器站点上传图像和发送一些文本POST方法

IT IS SELF ANSWER QUESTION

I want to send some texts with my image which want to upload, I know that Apache HttpClient multipart is deprecated. so I want to do this with HTTPUrlConnection . here my code below, please help.

  • 写回答

1条回答 默认 最新

  • doulezhi5326 2016-05-01 11:55
    关注

    I got the answer: I just rewrite my code (the correct one). hope help others with same trouble. I want to send an image with username and token, it is gonna like this in android and php :

    protected Object doInBackground(Object[] params) {
        int fbyte,buffersize,cbuffer;
        int maxbuffer=1024*1024;
        final String lineEnd = "
    ";
        String twoHyphens = "--";
        String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
        BufferedReader reader = null;
    
        try{
            String sfile = params[1].toString();
            File f=new File(sfile);
            FileInputStream fis=new FileInputStream(f);
            URL url=new URL(params[0].toString());
            HttpURLConnection con =(HttpURLConnection) url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setUseCaches(false);
            con.setRequestProperty("Connection", "keep-Alive");
            con.setRequestProperty("ENCType", "multipart/form-data");
            con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
            con.setRequestProperty("imagefile", sfile);
            DataOutputStream dos =new DataOutputStream(con.getOutputStream());
    
    
            // upload image :
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"imagefile\"; filename=\"" + sfile + "\""+ lineEnd);
            dos.writeBytes(lineEnd);
    
            fbyte=fis.available();
            buffersize=Math.min(fbyte, maxbuffer);
            byte[] buffer=new byte[buffersize];
            cbuffer=fis.read(buffer, 0, buffersize);
            while(cbuffer>0){
                dos.write(buffer,0,buffersize);
                fbyte=fis.available();
                buffersize=Math.min(fbyte, maxbuffer);
                cbuffer=fis.read(buffer,0,buffersize);
            }
            dos.writeBytes(lineEnd);
    
            String Username = "my username";
            String Token    = "my token";
    
            //write username :
            dos.writeBytes(twoHyphens+ boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"username\";" + lineEnd);
            dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
            dos.writeBytes(lineEnd + Username + lineEnd);
            dos.writeBytes(lineEnd);
    
            //write token :
            dos.writeBytes(twoHyphens+ boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"token\";" + lineEnd);
            dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
            dos.writeBytes(lineEnd + Token + lineEnd);
            dos.writeBytes(lineEnd);
    
            if(con.getResponseCode()==200){
                fis.close();
                dos.flush();
                dos.close();
            }
    
        }catch(final Exception e){ e.printStackTrace(); }
        return "";
    }// end doInBackground()
    

    php side :

    $username = $_POST['username'];
    $token = $_POST['token'];
    
    $target_path = "upload/". basename( $_FILES['imagefile']['name']);
    move_uploaded_file($_FILES['imagefile']['tmp_name'], $target_path);
    
    echo "username: ".$username." and token: ".$token;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗