android:uploaddatatoWeb

來源:互聯網
上載者:User

android:uploaddatatoWeb

                    


package com.example.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTool {public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();// 輸出資料流,寫到記憶體byte[] buffer = new byte[1024];// 1K緩衝區容量int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);// 從數組buffer中讀取從0到len的資料}inStream.close();return outStream.toByteArray();}}

package com.example.web;import java.net.HttpURLConnection;import java.net.URL;import java.util.Map;public class HttpRequest {public static void sendGetRequest(String path, Map params)throws Throwable {StringBuilder str = new StringBuilder();str.append('?');for (Map.Entry entry : params.entrySet()) {str.append(entry.getKey()).append('=').append(entry.getValue()).append('&');}str.deleteCharAt(str.length() - 1);URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);}}

package com.example.web;import java.util.HashMap;import java.util.Map;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 android.widget.Toast;import com.example.image.R;import com.example.web.HttpRequest;public class MainActivity extends Activity {private static final String TAG = "UploaddataToWen";private EditText titleText;private EditText timelengthText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_image);Button button = (Button) this.findViewById(R.id.button);titleText = (EditText) this.findViewById(R.id.title);timelengthText = (EditText) this.findViewById(R.id.timelength);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String title = titleText.getText().toString();String timelength = timelengthText.getText().toString();Map params = new HashMap();params.put("method", "save");params.put("title", title);params.put("timelength", timelength);try {HttpRequest.sendGetRequest("http://192.168.1.101:8080/web/admin.do", params);Toast.makeText(MainActivity.this, "save success",Toast.LENGTH_LONG).show();} catch (Throwable e) {Toast.makeText(MainActivity.this, "save fail",Toast.LENGTH_LONG).show();Log.e(TAG, e.toString());}}});}}// // 

/*package org.android.net;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;public class HtmlRequest {public static void main(String[] args) throws Throwable {URL url = new URL("http://www.baidu.con");HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);InputStream inStream = conn.getInputStream();// 通過輸入資料流擷取html資料byte[] data = readInputStream(inStream);// 得到的html的位元據String html= new String(data);       System.out.print(html);}public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();// 輸出資料流,寫到記憶體byte[] buffer = new byte[1024];// 1K緩衝區容量int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);// 從數組buffer中讀取從0到len的資料}inStream.close();return outStream.toByteArray();}}package org.android.net;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;public class ImageRequest {public static void main(String[] args) throws Throwable {URL url = new URL("http://imageaddress");HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);InputStream inStream = conn.getInputStream();// 通過輸入資料流擷取圖片資料byte[] data = readInputStream(inStream);// 得到的圖片的位元據File imageFile = new File("temp.jpg");FileOutputStream outStream = new FileOutputStream(imageFile);// 把圖片寫入到硬碟outStream.write(data);outStream.close();}public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();// 輸出資料流,寫到記憶體byte[] buffer = new byte[1024];// 1K緩衝區容量int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);// 從數組buffer中讀取從0到len的資料}inStream.close();return outStream.toByteArray();}}*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.