¾È±Ô °øºÎ¹æ

Android > HttpURLConnection - ÆÄÀÏ,ÅؽºÆ® ¼­¹ö¿¡ Àü¼Û ¿¹Á¦

µî·ÏÀÏ : 2017-07-06 17:36 Á¶È¸¼ö : 53,794

¼­¹ö¿¡ µ¥ÀÌÅÍ¿Í À̹ÌÁöÆÄÀÏÀ» Àü¼ÛÇÏ´Â ¿¹Á¦ ÀÔ´Ï´Ù.
 
		package com.example.study;

		import java.io.BufferedReader;
		import java.io.DataOutputStream;
		import java.io.FileInputStream;
		import java.io.IOException;
		import java.io.InputStreamReader;
		import java.net.HttpURLConnection;
		import java.net.URL;

		import android.app.Activity;
		import android.os.Bundle;
		import android.os.Handler;
		import android.os.Message;
		import android.util.Log;
		import android.view.View;
		import android.view.View.OnClickListener;
		import android.widget.Button;
		import android.widget.EditText;

		public class upload_test extends Activity {

			private Button mUploadBtn ;
			private FileInputStream mFileInputStream = null;
			private URL connectUrl = null;
			private EditText mEditEntry ;
			String lineEnd = "\r\n";
			String twoHyphens = "--";
			String boundary = "******";
			private DataOutputStream dos = null;
		   

			public String mFilePath = "/storage/extSdCard/ankyu.jpg"; 
			public String urlString = "http://ankyu.entersoft.kr/test/upload2.asp";
			
			public String reutn_txt = "";
			ProgressHandler  handler = new ProgressHandler();


			
			@Override
			public void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);
			
				setContentView(R.layout.uploadtest);
					
				mEditEntry = (EditText)findViewById(R.id.edit_entry);
				mUploadBtn = (Button)findViewById(R.id.btn);	            
				mUploadBtn.setOnClickListener( new OnClickListener() {
					 @Override
					 public void onClick(View v) {
						 // TODO Auto-generated method stub
						 try{
							 mEditEntry.setText("Uploading....") ;
							  // ¾÷·ÎµåÇÒ ÆÄÀÏÀ» ¹Ì¸® Áغñ ÇÒ°Í
							 DoFileUpload(mFilePath);
						 }catch(Exception e){
						 }
					 }
				});	    	    
			}
			

			private void DoFileUpload(final String mFilePath) throws IOException {
				// TODO Auto-generated method stub
				Log.d("File Up" , "file path = " + mFilePath);
								
				Runnable runnable1 = new background2();    	
				Thread thread = new Thread(runnable1);    	
				thread.start();    	
				
				

			}
			

			public class background2 implements Runnable {
				public void run() {

					String txt = "";
					//¿©±â¿¡ ÀÛ¾÷ÇÒ ³»¿ëÀ» ³Ö´Â´Ù.			
					try  {
						mFileInputStream = new FileInputStream(mFilePath);
						connectUrl = new URL(urlString);
						Log.d("File Up1" , "mFileInputStream is " + mFileInputStream);
						// open connection
						HttpURLConnection 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);
						 
						DataOutputStream dos = new DataOutputStream( conn.getOutputStream()) ;
						
						String fileName1 = "¾È±Ô.jpg";

						// write data
						//dos.writeBytes(twoHyphens + boundary + lineEnd);
						

						//ÅؽºÆ®°ª 
						StringBuffer pd = new StringBuffer();
						pd.append(twoHyphens + boundary + lineEnd);
						
						pd.append("Content-Disposition: form-data; name=\"board_title\""+lineEnd);
						pd.append(lineEnd);
						pd.append("°Ô½ÃÆÇ Á¦¸ñ2"+lineEnd);															
						
						pd.append(twoHyphens + boundary + lineEnd);
						pd.append("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""+fileName1+"\"\r\n");
						pd.append(lineEnd);


						Log.e("DATA" , "DATA => \r\n" + pd.toString());
											
						
						dos.writeUTF(pd.toString());

						//dos.writeBytes("Content-Disposition:form-data;name=\"uploadedfile\";filename=\"" + fileName1 + "\"" + lineEnd);
						//dos.writeBytes(lineEnd);
						
						int bytesAvailable = mFileInputStream.available();
						int maxBufferSize = 1024;
						int bufferSize = Math.min(bytesAvailable, maxBufferSize);
						byte[] buffer = new byte[bufferSize];
						int bytesRead = mFileInputStream.read( buffer , 0 , bufferSize);
						Log.d("File Up2", "image byte is " + bytesRead );
						// Read ÆÄÀÏ
						while(bytesRead > 0 ){
							dos.write(buffer , 0 , bufferSize);
							bytesAvailable = mFileInputStream.available();
							bufferSize = Math.min(bytesAvailable, maxBufferSize);
							bytesRead = mFileInputStream.read(buffer,0,bufferSize);
						}
				
						dos.writeBytes(lineEnd);
						dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
				
						//close streams
						
						Log.e("File Up3" , "File is written");
						mFileInputStream.close();
						dos.flush(); // ¹öÆÛ¿¡ ÀÖ´Â °ªÀ» ¸ðµÎ ¹Ð¾î³¿
						
						//À¥¼­¹ö¿¡¼­ °á°ú¸¦ ¹Þ¾Æ EditText ÄÁÆ®·Ñ¿¡ º¸¿©ÁÜ
						

						BufferedReader reader = null;
						
						String headerType = conn.getContentType();
						
						if (headerType.toUpperCase().indexOf("EUC-KR") != -1) {
							Log.e("DATA" , "return1 => " + headerType);
							 reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"EUC-KR"));
						} else if (headerType.toUpperCase().indexOf("UTF-8") != -1){
							Log.e("DATA" , "return2 => " + headerType);
							 reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
						}
						


						while((txt = reader.readLine()) != null) 
						{
								reutn_txt	= txt;
							 Log.e("reader:" , txt);
						}

						reader.close();
					   
						//mEditEntry.setText(txt);
						
						dos.close();
						
						//mEditEntry.setText("Àü¼Û ¿Ï·á!!!!!!"+txt) ;
						
				
						Message msg = handler.obtainMessage();	//È£ÃâÀÇ°á°ú·Î ¸Þ½ÃÁö °´Ã¼¸¦ ¸®ÅϹ޴´Ù.
						handler.sendMessage(msg);			//msg ¸¦ ProgressHandler Ŭ·¡½ºÀÇ handleMessage·Î Àü¼ÛÇÑ´Ù.

									
						
						
					} catch (Throwable t) 
					{
						// ¿À·ù°¡ ¹ß»ýÇÏ¸é ¹é±×¶ó¿îµå ½º·¹µå¸¦ ±×´ë·Î Á¾·á.
						Log.e("SampleJavaThread","Exception in thread.",t);    			
					}


				}


				
			}
			

			public class ProgressHandler extends Handler {
				public void handleMessage(Message msg) {
					
					//¹ÞÀº ¸Þ½ÃÁö¸¦ ó¸®ÇÑ´Ù.
		 
					
					mEditEntry.setText("Àü¼Û ¿Ï·á!"+reutn_txt) ;
					
					Log.e("SampleJavaThread","Exception in thread.");

				}
			}
		}




http://ankyu.entersoft.kr/test/upload2.asp  ¼Ò½º
set FSO					= Server.CreateObject("Scripting.FileSystemObject") 
  set UploadForm			= Server.CreateObject("ABCUpload4.XForm") 
  UploadForm.CodePage		= 65001
  UploadForm.Overwrite		= True														'µ¤¾î¾²±â Çã¿ë
  UploadForm.AbsolutePath	= True
  UploadForm.MaxUploadSize	= 1024*1024*10												'ÆÄÀÏ¿ë·® (10M)


  board_title 				= UploadForm("board_title")(1)


  '³¯Â¥ Æú´õ »ý¼º
  UP_DIR="/file_data"
  UP_DIR = Server.MapPath(UP_DIR)

  'ÆÄÀÏ÷ºÎ1
  Set theField = UploadForm("uploadedfile")(1)										'ÆÄÀÏÀÌ ¾÷·Îµå µÇ¾ú´Ù¸é 
  If theField.FileExists Then
		fileName	= theField.safeFileName											'ÆÄÀϸí
		fileSize	= theField.Length												'ÆÄÀÏ»çÀÌÁî

		if fileSize>(1024*1024*10) then 
			message =  "È­ÀÏ ¿ë·®ÀÌ 20MÀ» ÃÊ°úÇÒ¼ö ¾ø½À´Ï´Ù." 
		Else
			strname		= Mid(fileName,1,InstrRev(fileName,".")-1)
			strExt		= Lcase(Mid(filename,InstrRev(filename,".")+1))
			strname		= file_StrDelete(strname)										'ÆÄÀϸí Ư¼ö¹®ÀÚ »èÁ¦

			bExist		= True 
			strfilename = strname & "." & strExt 
			cntFile	    = 0 
			do while bExist 
				if (FSO.FileExists(UP_DIR&"/"&strfilename)) then
					cntfile		= cntfile+1
					strfilename	= strname&"_"&cntfile&"."&strExt  
				else 
					bExist		= false
				end if
			Loop		

			filename1	= strfilename 


			msg = filename1 &""


			theField.Save UP_DIR &"/"& filename1									'ÆÄÀÏÀúÀå


		End if
  End If
 
  IF len(trim(filename1))>1 THEN
    FILENAME1=CSTR(yea)+cstr(mon)+"/"+filename1
  end if   
	  
	  strSql = " INSERT INTO  board_test (board_title, board_fname1)"
	  strSql = strSql & " values ("
	  strSql = strSql & " N'"& board_title &"' "
	  strSql = strSql & ",N'"& filename1 &"') "

	  set objRs=objCnn.Execute(strSql)
  
  response.write msg

	'------------------- ƯÁ¤ ¹®ÀÚ »èÁ¦ ÇÔ¼ö -----------------------
	Function file_StrDelete(strmessage)
			StrDelete=Replace(strmessage,".","")
			StrDelete=Replace(StrDelete,"&","")
			StrDelete=Replace(StrDelete,"%","")
			StrDelete=Replace(StrDelete," ","")
			StrDelete=Replace(StrDelete,",","")
			StrDelete=Replace(StrDelete,"?","")
			StrDelete=Replace(StrDelete,"@","")
			StrDelete=Replace(StrDelete,"$","")
			StrDelete=Replace(StrDelete,"!","")
			StrDelete=Replace(StrDelete,"^","")
			StrDelete=Replace(StrDelete,"*","")
			StrDelete=Replace(StrDelete,"+","")
			StrDelete=Replace(StrDelete,";","")
			StrDelete=Replace(StrDelete,"/","")
			file_StrDelete=StrDelete
	End Function
 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.