어떤 분이 하신 파일 그대로 복사해서 돌려봤는데

textview에 아래의 핸들러 부분인  C#파일을 읽어 들입니다. 원래는 경로를 출력하는 건데 말이죠 그리고 저장이 어디로 되는지 도통 모르겠네요. upload란 폴더를 만들었는데도 저장이 안되네요

 

--upload.java 안드로이드 부분----

import! android.app.Activity;
import! android.os.Bundle;
import! android.util.Log;
import! android.view.View;
import! android.widget.Button;
import! android.widget.EditText;


import! java.io.DataOutputStream;
import! java.io.FileInputStream;
import! java.io.IOException;
import! java.io.InputStream;
import! java.net.HttpURLConnection;
import! java.net.URL;


public class UploadFile extends Activity {
    /** Called when the activity is first created. */
private Button mUploadBtn ;
private FileInputStream mFileInputStream = null;
private URL connectUrl = null;
private EditText mEditEntry ;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mEditEntry = (EditText)findViewById(R.id.edit_entry);
        mUploadBtn = (Button)findViewById(R.id.btn);
       
        mUploadBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
mEditEntry.setText("Uploading....") ;
String mFilePath = "/sdcard/t.jpg";  // 업로드할 파일을 미리 준비 할것
DoFileUpload(mFilePath);
}catch(Exception e){
}
}


});
    }
   
    private void DoFileUpload(String mFilePath) throws IOException {
// TODO Auto-generated method stub
Log.d("File Up" , "file path = " + mFilePath);
HttpFileUpload("http://192.168.11.215:1397/UploadFromAndroid.ashx" , "" , mFilePath );
}


private void HttpFileUpload(String urlString , String params, String fileName) {
// TODO Auto-generated method stub
try{
mFileInputStream = new FileInputStream(fileName);
connectUrl = new URL("urlString);
Log.d("File Up" , "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);
// write data
DataOutputStream dos = new DataOutputStream( conn.getOutputStream()) ;
   dos.writeBytes(twoHyphens + boundary + lineEnd);


// uploadedfile 파일이 ashx 핸들러에서 파일을 찾을 때 사용함으로 이름이 반드시 동일해야함..

// 이름을 바꾸면 ashx 파일에서도 바꿀것.

 


dos.writeBytes("Content-Disposition:form-data;name=\"uploadedfile\";filename=\"" + fileName + "\"" + 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 Up", "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 Up" , "File is written");
mFileInputStream.close();
dos.flush(); // 버퍼에 있는 값을 모두 밀어냄
//웹서버에서 결과를 받아 EditText 컨트롤에 보여줌
int ch;
InputStream is =conn.getInputStream();
StringBuffer b = new StringBuffer();
while((ch = is.read()) != -1 ){
b.append((char)ch);
}
String s = b.toString();
Log.e("File Up" , "result = " + s);
mEditEntry.setText(s);
dos.close();
}catch(Exception e)
{
Log.d("File Up" , "exception " + e.getMessage());
}
}
}
---UploadFromAndroid.ashx 핸들러 파일---

<%@ WebHandler Language="C#" Class="UploadFromAndroid" %>

 


using System;

using System.Web;

 


public class UploadFromAndroid : IHttpHandler {

   

    public void ProcessRequest (HttpContext context) {

        

        // 업로드할 파일 정의를 가지고 온다. uploadedfile은 안드로이드 소스 지정한 레이블임으로 반드시 동일해야함

        HttpPostedFile fp = context.Request.Files["uploadedfile"];

       

        // 저장할 폴더 - 웹서버의 Upload 디렉토리에 파일 저장(파일 업로드시에는 서버의 절대 경로가 반드시 필요함)

        string targetDirectory = context.Request.PhysicalApplicationPath + "upload" ;

     

       // 테스트를 위해 파일이름을 간단히 aaa.jpg로 했음.. 필요에 따라 이름을 지정하는 코드를 넣을것

        targetDirectory = targetDirectory + "\\web.html";

 


        try

        {

// File을 서버에 지장한 이름으로 저장함

            fp.SaveAs(targetDirectory);

        }

        catch (Exception e)

        { 

// 에러 발생 처리

context.Response.Write(e.Message);

return;

        }

        // 업로드 성공

        context.Response.Write("Uploaded =" + targetDirectory);

       

    }

 

    public bool IsReusable {

        get {

            return false;

        }

    }

}