Android
public void HttpFileUpload(String urlString, String params, String fileName) { String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { FileInputStream mFileInputStream = new FileInputStream(fileName); URL connectUrl = new URL(urlString); Log.d("Test", "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); 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("Test", "image byte is " + bytesRead); // read image 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("Test" , "File is written"); mFileInputStream.close(); dos.flush(); // finish upload... // get response 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("Test", "result = " + s); dos.close(); } catch (Exception e) { Log.d("Test", "exception " + e.getMessage()); // TODO: handle exception } }
ASP.net
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Test { public partial class get_image : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // 업로드할 파일 정의를 가지고 온다. uploadedfile은 안드로이드 소스 지정한 레이블임으로 반드시 동일해야함 HttpPostedFile fp = Request.Files["uploadedfile"]; // 저장할 폴더 - 웹서버의 Upload 디렉토리에 파일 저장(파일 업로드시에는 서버의 절대 경로가 반드시 필요함) string targetDirectory = Request.PhysicalApplicationPath + "Upload"; targetDirectory = targetDirectory + "\\aaa.jpg"; string file_name = fp.FileName; try { fp.SaveAs(targetDirectory); } catch (Exception ex) { Response.Write(ex.Message); return; } // 업로드 성공 Response.Write("Uploaded =" + targetDirectory); } } }
'Mobile Programming > Android' 카테고리의 다른 글
안드로이드 기기 해상도 (0) | 2012.06.04 |
---|---|
eclipse 실행 시 An error has occurred. See the log file (0) | 2012.05.06 |
안드로이드 핸드폰번호 가져오기 (0) | 2012.03.07 |
[Android] 퍼미션 종류별 정리 (0) | 2012.01.30 |
안드로이드 C2DM Push 구현하기 (Push Server) (0) | 2012.01.08 |
댓글