웹으로 파일 업로드 주소와 파일명은 각자조건에 맞게 수정해서 사용하세요.
public static String uploadFile(String sourceFileUri)
{
String upLoadServerUri = Global.UrlAddress+"/picture_register.php";
int serverResponseCode=0;
String doc = "";
String fileName = Global.user_data.userNo+".png";
Util.Log("file name:"+sourceFileUri);
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile())
{
return "";
}
else
{
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFileUri);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
Log("fileName : "+fileName);
dos = new DataOutputStream(conn.getOutputStream());
//////////////////////////////////////////////////////////////////////////////////////////
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"" + "userNo" + "\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(Global.user_data.userNo);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"" + "macAddr" + "\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(Global.user_data.d_macAddr);
dos.writeBytes(lineEnd);
/////////////////////////////////////////////////////////////////////////////////////////
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200)
{
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
sourceFile.delete();
String buffer2 = null;
BufferedReader in = new BufferedReader(new InputStreamReader
(conn.getInputStream()));
while ((buffer2 = in.readLine()) != null)
{
doc = doc + buffer2 + "\n";
}
in.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
}
Util.Log("doc : "+doc);
doc= java.net.URLDecoder.decode(doc);
Util.Log("doc decoder : "+doc);
return doc;
} // End else block
}
'ANDROID' 카테고리의 다른 글
bitmap to byte 비트맵을 바이트정보로. (0) | 2014.10.11 |
---|---|
android 치트 막기. (0) | 2014.10.11 |
bmp to png 비트맵을 png파일로 저장하자. (0) | 2014.10.11 |
인터넷 상태 확인 네트워크 상태. (0) | 2014.10.11 |
안드로이드 맥어드레스 얻어오기 macaddress (0) | 2014.10.11 |