2014-10-18 2 views
0

특정 폴더의 모든 파일을 업로드하고 싶습니다. 다음 코드에서 배열의 파일 목록을 저장했습니다.android에 여러 파일을 업로드하는 방법

String path = Environment.getExternalStorageDirectory().toString()+"/backup/";  
File f = new File(path);   
File file[] = f.listFiles(); 
int fileNum = file.length; 
String[] fileName = new String[fileNum]; 
for (int i=0; i < file.length; i++) {        
    fileName[i] = file[i].getName();         
} 

하나의 파일을 업로드하는 데 다음 코드가 있습니다. 배열 파일 이름에서 코드 나 나 루프를 할 경우 내가 어떤 부분을 제거

package com.example.uploadfile; 

import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    TextView messageText; 
    private Button uploadButton; 
    String upLoadServerUri = null; 
    int serverResponseCode = 0; 

    final String uploadFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/backup/"; 
    String uploadFileName = "sand.jpg"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     upLoadServerUri = "http://www.example.com/UploadToServer.php"; 

     messageText = (TextView) findViewById(R.id.messageText); 
     uploadButton = (Button) findViewById(R.id.uploadButton);    

     uploadButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       new Thread(new Runnable() { 
        public void run() {      
         uploadFile(uploadFilePath + "" + uploadFileName); 
        } 
       }).start(); 
      } 
     }); 
    } 
    public int uploadFile(String sourceFileUri) { 
     String fileName = sourceFileUri; 

     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 File(sourceFileUri); 

     try {   
      FileInputStream fileInputStream = new FileInputStream(sourceFile); 
      URL url = new URL(upLoadServerUri); 

      conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setUseCaches(false); 
      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("uploaded_file", fileName); 

      dos = new DataOutputStream(conn.getOutputStream()); 
      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;filename=" + fileName + "" + lineEnd); 
      dos.writeBytes(lineEnd); 

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

      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); 
      } 

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

      serverResponseCode = conn.getResponseCode(); 
      final String serverResponseMessage = conn.getResponseMessage(); 

      Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); 

      if (serverResponseCode == 200) { 
       runOnUiThread(new Runnable() { 
        public void run() { 
         String msg = "File Upload Completed.\n"; 
         msg += "See uploaded file at\n\nhttp://www.example.com/uploads/" + uploadFileName; 

         messageText.setText(msg); 
         Toast.makeText(MainActivity.this, "File Upload Complete:"+serverResponseMessage, Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      }   
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
     } catch (Exception e) {   
      e.printStackTrace(); 
     }  
     return serverResponseCode; 
    } 
} 

답변

2
String path = Environment.getExternalStorageDirectory().toString()+"/backup/";  
    File f = new File(path);   
    File file[] = f.listFiles(); 
    for (int i=0; i < file.length; i++) {        
     // upload file here and when the fileUpload is complete the you save it to the array.. thats wat i think is best.. because you never know 
     //Thread or asynctask       
    } 

는 해당 배열에있는 모든 파일을 업로드 할 수 있도록. 주위에있는 나의 길 때문에 ... 당신은 그것을 추가하고 그것 주위에 당신의 길을 만들어 줄 수 있습니다. ..Lol 그것이 의미가 있기를 바란다.

+0

당신은 완전한 일부를 더할 수 있냐? @ 엘츠 – Banku

관련 문제