마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드에서 php서버로 이미지 전송하는법

0 추천

사진을 찍었을떼, 

byte[]값을 받아서 php서버로 전송하는 부분을 구현해보고 있는데요;

test.jpg파일이 생성은 되는데, 크기가 0이네요;;

 

어디가 잘못된걸까요?;;ㅠㅜㅜ

	 public void doFileUpload(byte[] data) {
	       try {
	        URL url = new URL("http://oculus.dothome.co.kr/imgserv.php");
	        Log.i(TAG, "http://oculus.dothome.co.kr/imgserv.php" );
	        String lineEnd = "\r\n";
	        String twoHyphens = "--";
	        String boundary = "*****"; 

	        // open connection 
	        HttpURLConnection con = (HttpURLConnection)url.openConnection();   
	        con.setDoInput(true); //input 허용
	        con.setDoOutput(true);  // output 허용
	        con.setUseCaches(false);   // cache copy를 허용하지 않는다.
	        con.setRequestMethod("POST");
	        con.setRequestProperty("Connection", "Keep-Alive");
	        con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
	        
	        // write data
	        DataOutputStream dos = 
	                             new DataOutputStream(con.getOutputStream());
	        Log.i(TAG, "Open OutputStream" );
	        dos.writeBytes(twoHyphens + boundary + lineEnd);


	        
	        // 파일 전송시 파라메터명은 file1 파일명은 camera.jpg로 설정하여 전송
	        dos.writeBytes(twoHyphens + boundary + lineEnd);
	        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + FileName+"\"" + lineEnd);

	      
	        dos.writeBytes(lineEnd);
	        dos.write(data,0,data.length);
	        Log.i(TAG, data.length+"bytes written" );
	        dos.writeBytes(lineEnd);
	        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
	        dos.flush(); // finish upload...   
	        
	       } catch (Exception e) {
	        Log.i(TAG, "exception " + e.getMessage());
	        // TODO: handle exception
	       } 
	       Log.i(TAG, data.length+"bytes written successed ... finish!!" );
	      
	     

	    }  

 

 

그리고 php 서버를 닷홈에서 호스팅을 무료로 받아서 이용하고 있는데요

 

처음엔 초기에 있는 폴더나 파일들을 냅두고 걍 사용을 했엇는데, 파일업로드가 잘 안되서 

기존에 있는 폴더나 파일들을 다 지운 상태로 imgserv.php만 남겨놨더니

아에 접근을 못하게 되버렸네요 ㅠㅠ

 

<?php
$target_path = "files/";

$tmp_img = explode("." ,$_FILES['uploadedfile']['name']); 
$img_name = $tmp_img[0]."_".date('Y-m-d_H_i_s').".".$tmp_img[1];
$target_path = $target_path . basename($img_name);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ".$img_name." has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}

?>

서버에서는 이렇게 받아줍니다.

 

 

아우아디 (1,900 포인트) 님이 2014년 10월 14일 질문

1개의 답변

0 추천
저같은 경우에는 아파치 FTP 라이브러리 썻었는데

그거 사용하시면 안될까요?

http://pigserver.tistory.com/category/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D%20%EA%B0%95%EC%A2%8C/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C

요걸론 도움이 안되시려나요 ㅠㅠ
갸아악 (21,260 포인트) 님이 2014년 10월 14일 답변
음...링크가 이상하게 걸린듯 합니다...
...