将文件和字符串上传到php服务器

时间:2014-12-15 16:24:55

标签: php android file-upload html-form-post

我想将文件和字符串上传到PHP服务器上的HTML表单。我用下面的代码上传了文件,但我不知道如何上传字符串。我应该如何修改此代码以将字符串上载到表单的文本字段? 我的HTML表单:

    <form name="form" id="gallery" method="post" enctype="multipart/form-data" action="send_1.php">
<div id="contain" >
        <div class='file_browse_wrapper' id="file1">
            <input type="file" size="32" name="file1" value=""  />
            <input type="file" size="32" name="file2" value=""  />           
            <input type="text" size="32" name="phonenumber" value="1234" />
            <input type="text" size="32" name="uploadmessage" value="some text" />
        </div>
    </div><br>
    <input type="submit" name="add" id="add" value="click me" /><!--end add-->
</form><!--end form-->

我能够使用以下代码上传文件:

 public int uploadFiles(int pictureNumber, String strToUpload1, String strToUpload2) {
     String uploadServerUri;
     String uploadFilePath = Environment.getExternalStorageDirectory().toString() + "/Pictures/MyCameraApp/";
     final int timeStamp;
     int serverResponseCode = 0;
     HttpURLConnection conn = null;
     int bytesRead, bytesAvailable, bufferSize;
     byte[] buffer;
     int maxBufferSize = 1 * 1024 * 1024;
     String lineEnd = "\r\n";
     String twoHyphens = "--";
     String boundary = "*****";
     DataOutputStream doc = null;
     FileInputStream fileInputStream = null;
     String[] paths = new String[2];

     for(int i=0; i<2; i++) {
         paths[i] = uploadFilePath + timeStamp +"-" + Integer.toString( pictureNumber + i ) + ".jpg" ;
      }
      try {
          URL url = new URL(uploadServerUri);
          conn = (HttpURLConnection) url.openConnection();
          conn.setDoInput(true); // Allow Inputs
          conn.setDoOutput(true); // Allow Outputs
          conn.setUseCaches(false); // Don't use a Cached Copy
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Connection", "Keep-Alive");
          conn.setRequestProperty("ENCTYPE", "multipart/form-data");
          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
          for(int i=0; i<2; i++)
              conn.setRequestProperty("uploadedfile" + Integer.toString(i), paths[i]);
          for(int i=0; i<2; i++) {
              File sourceFile = new File(paths[i]);
              fileInputStream = new FileInputStream(sourceFile);
              Log.d(TAG, ".....start uploading file"+ Integer.toString(i));
              doc = new DataOutputStream(conn.getOutputStream());
              doc.writeBytes(twoHyphens + boundary + lineEnd);
              doc.writeBytes("Content-Disposition: form-data; name=\"uploadedfile" + (i + 1) + "\"" + ";filename=\""
                      + paths[i] + "\"" + lineEnd);
              doc.writeBytes(lineEnd);
              // create a buffer of  maximum size
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              buffer = new byte[bufferSize];
              // read file and write it into form...
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              while (bytesRead > 0) {
                  doc.write(buffer, 0, bufferSize);
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              }
              // send multipart form data necesssary after file data...
              doc.writeBytes(lineEnd);
              doc.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);      
              Log.d(TAG, ".....upload file "+Integer.toString(i)+" completed");
              fileInputStream.close();
              doc.flush();
          }//end of for
          // Responses from the server (code and message)
          serverResponseCode = conn.getResponseCode();
          String serverResponseMessage = conn.getResponseMessage();
          Log.d(TAG, "HTTP Response is : "
                  + serverResponseMessage + ": " + serverResponseCode);
          if(serverResponseCode == 200)
              Log.d(TAG, "upload completed");
          doc.close();
      }//end of first try
      catch (MalformedURLException e) {
          Log.e(TAG, e.getMessage().toString()+"..."+"MalformedURLException Exception : check script url.");
      }
      catch (Exception e) {
          Log.e(TAG, e.getMessage().toString());
      }
      return serverResponseCode;      

}

0 个答案:

没有答案