안녕하세요!

안드로이드앱에서 서버로 파일전송 질문입니다



파일을 한번에 여러파일을 전송할려고 합니다.

그런데 전송은 되는데 

파일을 받은 서버에서 파일을 열어서 확인하면 첫번째 전송받은 파일만 정상이고 

나머지 부터는 파일을 열어서 확인하면 전송중에 깨진건지 파일이 깨져서 열리지가 않습니다.



혹시 이런경우 해결하신분 계실까요

대용량파일을 전송하기 위해서 아래와같은 방식으로 구현을 해야 한다고 해서 수정을 했는데 잘되지 않내요

몇칠째 삽질중입니다.. ^^;



final static String boundary = "0xKhTmLbOuNdArY";

public static HttpURLConnection getURLConnection(String urlString) {

     HttpURLConnection conn = null;

     try {

            final URL connectURL = new URL("getServer() + urlString);


            conn = (HttpURLConnection) connectURL.openConnection();

            conn.setDoInput(true);

            conn.setDoOutput(true);

            conn.setUseCaches(false);

            conn.setRequestMethod("POST");

            conn.setRequestProperty("Connection", "Keep-Alive");

            conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);

            conn.setChunkedStreamingMode(0);


    } catch (Exception e) {

      e.printStackTrace();

    }

    return conn;

}


=======================================================================


DataOutputStream dos = null;

final HttpURLConnection conn = getURLConnection(urlString);

try {

    dos = new DataOutputStream(conn.getOutputStream());

    int fileCnt = 1;


    for ( JSONObject fileJsonObj : filesJsonObj ) {

             final Iterator<?> filekey = fileJsonObj.keys();

             dos.writeBytes("\r\n--" + boundary + "\r\n");

             Log.i("TEST","\r\n--" + boundary + "\r\n");

            while(filekey.hasNext()){

                  final String key = (String)filekey.next();

                  dos.writeBytes("Content-Disposition: form-data; name=\""+key+"\"; filename=\""+ fileJsonObj.getString(key) + "\";");

                  dos.writeBytes("Content-Type: application/octet-stream\r\n\r\n[Binary Data...]\r\n");

                  dos.flush();

                  Log.d("TEST", "Content-Disposition: form-data; name=\""+key+"\"; filename=\""+ fileJsonObj.getString(key) + "\";" );

                  Log.d("TEST", "Content-Type: application/octet-stream\r\n\r\n[Binary Data...]\r\n");

                  

                  final File file = new File(fileJsonObj.getString(key));

                  final FileInputStream fileInputStream = new FileInputStream(file);

                 

                  // create a buffer of maximum size

                  int bytesAvailable = fileInputStream.available();

                  final int maxBufferSize = 1024;

                  int bufferSize = Math.min(bytesAvailable, maxBufferSize);

                  final byte[] buffer = new byte[bufferSize];

                  // read file and write it into form...

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

                     

                }

         

           

                fileInputStream.close();

                dos.flush();

           }

    }

// close streams

// fileInputStream.close();


    dos.writeBytes("\r\n--" + boundary + "--" + "\r\n");

    Log.i("TEST","\r\n--" + boundary + "--" + "\r\n");

    dos.flush();


    dos.close();

} catch (Exception ex) {

Log.e("TEST", "URL error: " + ex.getMessage(), ex);

} finally{

    try{

         if(dos != null ){dos.close();}

    }catch(Exception e){

         e.printStackTrace();

    }

}


=======================================================================