Advertisement
Guest User

Untitled

a guest
Dec 15th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package org.nhnnext.android.nextagram;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9.  
  10. import android.util.Log;
  11.  
  12. public class ProxyUP {
  13.    
  14.  
  15.     private String lineEnd = "\r\n";
  16.     private String twoHyphens = "--";
  17.     private String boundary = "*****";
  18.    
  19.     public String uploadArticle(Article article, String filePath) {
  20.        
  21.         // 안드로이드 베이직 수업에서 자세히 다룰 내용입니다.
  22.         // 네트워크 선행이 필요한 이유입니다.
  23.        
  24.         try {
  25.            
  26.             FileInputStream fis = new FileInputStream(filePath);
  27.            
  28.             URL url = new URL("http://10.73.44.93/~stuXX/upload.php");
  29.            
  30.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  31.            
  32.             conn.setRequestProperty("Accept-Charset", "UTF-8");
  33.            
  34.             conn.setRequestMethod("POST");
  35.             conn.setRequestProperty("Cache-Control", "no-cache");
  36.             conn.setRequestProperty("Connection", "Keep-Alive");
  37.            
  38.             //응답 데이터가 많을 때에는 일정양 씩 묶어서 보내겠다는 의미
  39.             conn.setRequestProperty("Transfer-Encoding", "chunked");
  40.            
  41.             conn.setDoOutput(true);
  42.             conn.setDoInput(true);
  43.  
  44.             //Content는 multipart형식이고 데이터의 끝을 boundary(*****)로 표시를 하겠다.(content-length이 없으므로...)
  45.             conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
  46.  
  47.             // write data
  48.             DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
  49.            
  50.            
  51.             dos.write( getPostData("title",article.getTitle()).getBytes("UTF-8") );
  52.             dos.write( getPostData("writer",article.getWriter()).getBytes("UTF-8") );
  53.             dos.write( getPostData("id",article.getId()).getBytes("UTF-8") );
  54.             dos.write( getPostData("content",article.getContent()).getBytes("UTF-8") );
  55.             dos.write( getPostData("writeDate",article.getWriteDate()).getBytes("UTF-8") );
  56.             dos.write( getPostData("imgName",article.getImgName()).getBytes("UTF-8") );
  57.            
  58.            
  59.             dos.writeBytes(twoHyphens + boundary + lineEnd);
  60.             dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + article.getImgName() + "\"" + lineEnd);
  61.             dos.writeBytes(lineEnd);
  62.  
  63.  
  64.            
  65.             int bytesAvailable = fis.available();
  66.             int maxBufferSize = 1024;
  67.            
  68.             // 버퍼사이즈 조정
  69.             int bufferSize = Math.min(bytesAvailable, maxBufferSize);
  70.  
  71.             byte[] buffer = new byte[bufferSize];
  72.             int bytesRead = fis.read(buffer, 0, bufferSize);
  73.  
  74.             // read image
  75.             while (bytesRead > 0) {
  76.                 dos.write(buffer, 0, bufferSize);
  77.                 bytesAvailable = fis.available();
  78.                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
  79.                 bytesRead = fis.read(buffer, 0, bufferSize);
  80.             }
  81.  
  82.             dos.writeBytes(lineEnd);
  83.             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  84.  
  85.             // close streams
  86.             Log.i("Test", "File is written");
  87.            
  88.             fis.close();
  89.             dos.flush();
  90.             dos.close();
  91.            
  92.             int status = conn.getResponseCode();
  93.             Log.i("test", "statusUP:" + status);
  94.            
  95.             switch (status) {
  96.             case 200:
  97.             case 201:
  98.                 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  99.                 StringBuilder sb = new StringBuilder();
  100.                 String line;
  101.                 while ((line = br.readLine()) != null) {
  102.                     sb.append(line + "\n");
  103.                 }
  104.                 br.close();
  105.                 return sb.toString();
  106.             }
  107.  
  108.         } catch (Exception e) {
  109.             e.printStackTrace();
  110.             Log.i("test", "UPLOAD ERROR:" + e);
  111.         }
  112.        
  113.         return null;
  114.     }
  115.    
  116.     // post방식에 맞게 데이터 형식을 추가
  117.     private String getPostData(String key, String value) {
  118.         String result = twoHyphens + boundary + lineEnd;
  119.         result += "Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd;
  120.         result += lineEnd;
  121.        
  122.         result += value;
  123.        
  124.         result += lineEnd;
  125.        
  126.         return result;
  127.     }
  128.    
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement