Android使用AsyncTask加http用post方式上傳圖片到服務器

android端代碼展示:


 /* 上傳文件至Server的方法 */
    private void uploadFile() {
        System.out.print("正在發送請求!");
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        try {

            String urlpost=actionUrl;
            URL url = new URL(urlpost);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
			/* 允許Input、Output,不使用Cache */
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
			/* 設置傳送的method=POST */
            con.setRequestMethod("POST");
			/* setRequestProperty */
            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
			/* 設置DataOutputStream */
            DataOutputStream ds = new DataOutputStream(con.getOutputStream());
            ds.writeBytes(twoHyphens + boundary + end);
            ds.writeBytes("Content-Disposition: form-data; "
                    + "name=\"file1\";filename=\"" + newName + "\"" + end);


            ds.writeBytes(end);
			/* 取得文件的FileInputStream */
            FileInputStream fStream = new FileInputStream(uploadFile);
			/* 設置每次寫入1024bytes */
            System.out.print("已經找到數據正在發送!");
            int bufferSize = 1024*10;
            byte[] buffer = new byte[bufferSize];
            int length = -1;
			/* 從文件讀取數據至緩衝區 */
            while ((length = fStream.read(buffer)) != -1) {
				/* 將資料寫入DataOutputStream中 */
                ds.write(buffer, 0, length);
            }
            ds.writeBytes(end);
            ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

			/* close streams */
            fStream.close();
            ds.flush();
			/* 取得Response內容 */
            InputStream is = con.getInputStream();
            int ch;
            StringBuffer b = new StringBuffer();
            while ((ch = is.read()) != -1) {
                b.append((char) ch);
            }
			/* 將Response顯示於Dialog */

			/* 關閉DataOutputStream */
            ds.close();
            try {
                InfoUrl=InfoUrl+"?username="+username.getText()+"&password="+password.getText()+"&img="+b.toString();

                Log.i("login", "uploadFile: "+InfoUrl);
                HttpURLConnection huc= (HttpURLConnection) new URL(InfoUrl).openConnection();
                huc.setDoInput(true);
                huc.setDoOutput(true);
                huc.setUseCaches(false);
			/* 設置傳送的method=POST */
                huc.setRequestMethod("GET");
                huc.connect();
                InputStream is1 = huc.getInputStream();
                int ch1;
                StringBuffer b1 = new StringBuffer();
                while ((ch1 = is1.read()) != -1) {
                    b1.append((char) ch1);
                }
                int result=Integer.parseInt(b1.toString());

                if(result>0) {
                    showDialog("註冊成功,請登錄!");
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);

                }else {
                    showDialog("註冊失敗,請檢查!");
                }



            } catch (Exception e) {
                e.printStackTrace();
            }


        } catch (Exception e) {
            System.out.print("網絡出現異常!");
            showDialog("上傳失敗");
            e.printStackTrace();
        }
    }

服務器端:
PrintWriter out = response.getWriter();
		
		FileUploadTool fut=new FileUploadTool(request,"photo");
		
		String name=fut.getParameter("file1");
		
		
		out.print(name);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章