Tengo en mi aplicación una función para subir ficheros al servidor, esta función funciona de maravilla, pero tengo la problemática que junto al fichero tengo que mandar un parámetro extra llamado objeto , mi pregunta es : que cambio tendría que hacerle a mi función de subir el archivo para que también mande el otro parámetro.
Función que utilizo para subir el fichero
public void uploadFile(){
try {
Log.e("Service","se ejecuto");
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new UtilHelper().Compress(activity,command,profileBitmap,"");
String sourceFileUri = new UtilHelper().genString(activity)+".zip";
if (sourceFile.isFile()) {
try {
// String upLoadServerUri = "https://apretaste.com/run/app";
String upLoadServerUri = "http://192.168.137.1/demo/u.php";
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
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);
conn.setRequestProperty("file", sourceFileUri);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
+ sourceFileUri + "\"" + lineEnd);
dos.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) {
dos.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...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
if (serverResponseCode == 200) {
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder result = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
/*Gson gson = new Gson();
HttpInfo httpInfo;
httpInfo = gson.fromJson(result.toString(),HttpInfo.class);
if (httpInfo.code.equals("200")){
Log.e("link descarga",httpInfo.file);
downloadFile(httpInfo.file);
}*/
Log.e("respuesta",result.toString());
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
PHP que recibe los datos
<?php
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$uploads_dir = './upload/';
$tmp_name = $_FILES['file']['tmp_name'];
$pic_name = $_FILES['file']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
echo "subido";
}
else{
echo "File not uploaded successfully.";
}
echo $_POST['objeto'];
?>