2013-09-27 32 views
0

我不是PHP专家,因此我从互联网和本页面的问题中了解了很多东西。我需要发送一个文件到WAMP服务器(唱歌的PHP脚本),并发送一个字符串以创建目标路径,这取决于日期和发送的字符串。 目前,目标路径仅基于实际日期。例如2013-09-27,但我希望成为2013-09-27-XX,其中XX是设备发送到服务器以及上传文件的字符串。Android:上传文件并使用JSON/PHP发送字符串

这里是我用来上传文件到服务器的代码。

public void upload() throws Exception { 
     HttpURLConnection connection = null; 
     DataOutputStream outputStream = null; 

     String pathToOurFile = "/sdcard/"+Ident.getDNI()+"_"+Nombres.getNom()+"_"+Nombres.getApe1()+"_"+Nombres.getApe2()+".xml"; 
     String urlServer = "http://10.0.0.15/subida/upload_file.php"; 

     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 

     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1 * 1024 * 1024; 

      FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 
      URL url = new URL(urlServer); 

      connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      connection.setUseCaches(false); 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Connection", "Keep-Alive"); 
      connection.setRequestProperty("Content-Type", 
        "multipart/form-data;boundary=" + boundary); 

      outputStream = new DataOutputStream(connection.getOutputStream()); 
      outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
      outputStream 
        .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" 
          + pathToOurFile + "\"" + lineEnd); 
      outputStream.writeBytes(lineEnd); 

      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 

      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      while (bytesRead > 0) { 
       outputStream.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      } 

      outputStream.writeBytes(lineEnd); 
      outputStream.writeBytes(twoHyphens + boundary + twoHyphens 
        + lineEnd); 

      String serverResponseMessage = connection.getResponseMessage(); 


      fileInputStream.close(); 
      outputStream.flush(); 
      outputStream.close(); 
    } 

这里是PHP脚本我用得到的文件(根据日期):我想我应该在这里添加类似$SentString = $_POST('sentstring),然后将其添加到目标路径

<?php 
mkdir($target_path.date("Y-m-d")); 
$target_path = $target_path .date("Y-m-d") ."/" .basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
echo "El fichero ". basename($_FILES['uploadedfile']['name'])." ha sido enviado"; 
} else{ 
echo "Hubo un error, inténtalo de nuevo!"; 
} 
?> 

$target_path = $target_path .date("Y-m-d")-$SentString ."/" .basename($_FILES['uploadedfile']['name']); 

但是,我应该添加到Android客户端部分?

谢谢您的时间

回答

1

虽然看着你的代码,这是多要求,你可以使用多库上传多文件。

下载库下面

apache-mime4j-0.6.jar 
httpclient-4.1.jar 
httpcore-4.1.jar 
httpmime-4.1.jar 

和下面的几行代码上传代码

try { 
       HttpClient httpClient = new DefaultHttpClient(); 
       httpClient.getParams().setParameter(
         CoreProtocolPNames.PROTOCOL_VERSION, 
         HttpVersion.HTTP_1_1); 
       HttpPost post = new HttpPost(url); 

       post.setHeader("Content-Type", "image/jpg"); 

       MultipartEntity entity = new MultipartEntity(); 
       entity.addPart("image", new FileBody(new File(filePath))); 
       post.setEntity(entity); 
       try { 
        HttpResponse response = httpClient.execute(post); 
        System.out.println("response -- " + response.getEntity().getContent()); 

       } catch (IOException e) { 
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
       } 

使用下面的字符串添加字符串数据

entity.addPart("date", new StringBody("date")); 
+0

你好感谢你的帮助,但是还有一种方法仍在使用我目前使用的代码? – Katherine99

+0

实际上这个库很容易从客户端使用,数据也很容易从服务器端解析。正如你在实体中看到的那样,我们也可以发送字符串值对。所以根据我。代码不会超过5分钟。 –