• 安卓post 提交图片流和字符数据


    <?php 
        $target_path  = "./upload/";//接收文件目录 
        $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
           echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
        }  else{ 
           echo "There was an error uploading the file, please try again!" . $_FILES['uploadedfile']['error']; 
        } 
        ?>

    android

    package com.figo.uploadfile;
     
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class UploadfileActivity extends Activity
    {
      // 要上传的文件路径,理论上可以传输任何文件,实际使用时根据需要处理
      private String uploadFile = "/sdcard/testimg.jpg";
      private String srcPath = "/sdcard/testimg.jpg";
      // 服务器上接收文件的处理页面,这里根据需要换成自己的
      private String actionUrl = "http://10.100.1.208/receive_file.php";
      private TextView mText1;
      private TextView mText2;
      private Button mButton;
     
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        mText1 = (TextView) findViewById(R.id.myText2);
        mText1.setText("文件路径:
    " + uploadFile);
        mText2 = (TextView) findViewById(R.id.myText3);
        mText2.setText("上传网址:
    " + actionUrl);
        /* 设置mButton的onClick事件处理 */
        mButton = (Button) findViewById(R.id.myButton);
        mButton.setOnClickListener(new View.OnClickListener()
        {
          @Override
          public void onClick(View v)
          {
            uploadFile(actionUrl);
          }
        });
      }
     
      /* 上传文件至Server,uploadUrl:接收文件的处理页面 */
      private void uploadFile(String uploadUrl)
      {
        String end = "
    ";
        String twoHyphens = "--";
        String boundary = "******";
        try
        {
          URL url = new URL(uploadUrl);
          HttpURLConnection httpURLConnection = (HttpURLConnection) url
              .openConnection();
          // 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
          // 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
          httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
          // 允许输入输出流
          httpURLConnection.setDoInput(true);
          httpURLConnection.setDoOutput(true);
          httpURLConnection.setUseCaches(false);
          // 使用POST方法
          httpURLConnection.setRequestMethod("POST");
          httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
          httpURLConnection.setRequestProperty("Charset", "UTF-8");
          httpURLConnection.setRequestProperty("Content-Type",
              "multipart/form-data;boundary=" + boundary);
     
          DataOutputStream dos = new DataOutputStream(
              httpURLConnection.getOutputStream());
          dos.writeBytes(twoHyphens + boundary + end);
          dos.writeBytes("Content-Disposition: form-data; name="uploadedfile"; filename=""
              + srcPath.substring(srcPath.lastIndexOf("/") + 1)
              + """
              + end);
          dos.writeBytes(end);
     
          FileInputStream fis = new FileInputStream(srcPath);
          byte[] buffer = new byte[8192]; // 8k
          int count = 0;
          // 读取文件
          while ((count = fis.read(buffer)) != -1)
          {
            dos.write(buffer, 0, count);
          }
          fis.close();
     
          dos.writeBytes(end);
          dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
          dos.flush();
     
          InputStream is = httpURLConnection.getInputStream();
          InputStreamReader isr = new InputStreamReader(is, "utf-8");
          BufferedReader br = new BufferedReader(isr);
          String result = br.readLine();
     
          Toast.makeText(this, result, Toast.LENGTH_LONG).show();
          dos.close();
          is.close();
     
        } catch (Exception e)
        {
          e.printStackTrace();
          setTitle(e.getMessage());
        }
      }
    }

    参数比较全的

    public static String post(String actionUrl, Map<String, String> params, 
            Map<String, File> files) throws IOException { 
          StringBuilder sb2 = new StringBuilder(); 
          String BOUNDARY = java.util.UUID.randomUUID().toString();
          String PREFIX = "--" , LINEND = "
    ";
          String MULTIPART_FROM_DATA = "multipart/form-data"; 
          String CHARSET = "UTF-8";
     
          URL uri = new URL(actionUrl); 
          HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); 
          conn.setReadTimeout(5 * 1000); 
          conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setUseCaches(false); 
          conn.setRequestMethod("POST"); 
          conn.setRequestProperty("connection", "keep-alive"); 
          conn.setRequestProperty("Charsert", "UTF-8"); 
          conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); 
     
          StringBuilder sb = new StringBuilder(); 
          for (Map.Entry<String, String> entry : params.entrySet()) { 
            sb.append(PREFIX); 
            sb.append(BOUNDARY); 
            sb.append(LINEND); 
            sb.append("Content-Disposition: form-data; name="" + entry.getKey() + """ + LINEND);
            sb.append("Content-Type: text/plain; charset=" + CHARSET+LINEND);
            sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
            sb.append(LINEND);
            sb.append(entry.getValue()); 
            sb.append(LINEND); 
          } 
     
          DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); 
          outStream.write(sb.toString().getBytes()); 
          if(files!=null){
            //int i = 0;
            for (Map.Entry<String, File> file: files.entrySet()) { 
              StringBuilder sb1 = new StringBuilder(); 
              sb1.append(PREFIX); 
              sb1.append(BOUNDARY); 
              sb1.append(LINEND); 
              //sb1.append("Content-Disposition: form-data; name="file"+(i++)+""; filename=""+file.getKey()+"""+LINEND);
              sb1.append("Content-Disposition: form-data; name="userupfile"; filename=""+file.getKey()+"""+LINEND);
              sb1.append("Content-Type: application/octet-stream; charset="+CHARSET+LINEND);
              sb1.append(LINEND);
              outStream.write(sb1.toString().getBytes()); 
     
              InputStream is = new FileInputStream(file.getValue());
              byte[] buffer = new byte[1024]; 
              int len = 0; 
              while ((len = is.read(buffer)) != -1) { 
                outStream.write(buffer, 0, len); 
              }
     
              is.close(); 
              outStream.write(LINEND.getBytes()); 
            } 
          }
           
          byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); 
          outStream.write(end_data); 
          outStream.flush(); 
     
          int res = conn.getResponseCode(); 
          InputStream in = null;
          if (res == 200) {
            in = conn.getInputStream(); 
            int ch; 
             
            while ((ch = in.read()) != -1) { 
              sb2.append((char) ch); 
            } 
            Log.i("CAMERA", sb2.toString());
          }
           
          return in == null ? null : sb2.toString(); 
        }
  • 相关阅读:
    rsyslog服务日志收集配置
    Python urllib2 发送HTTP Request
    Jenkins版本回滚
    C++算法的40个高频面试问题集锦
    Linux添加自启动daemon service
    python爬虫随机设备信息生成模板
    selenium配置有账号密码验证的代理
    app脱壳后多个dex合并成一个
    webpack优化系列-多进程打包thread-loader
    AtCoder abc158_f
  • 原文地址:https://www.cnblogs.com/xiaoli3007/p/4222192.html
Copyright © 2020-2023  润新知