前言,bitmap位圖,後綴名.bmp/.dip,圖片的一種編碼格式,一張圖片由多個像素組成,每一個像素能夠表達多種色彩。bitmap是inputStream、drawable、byte[]、outputStream的中轉站,能夠轉換成某種。ImageView.setImageBitmap(Bitmap)能夠顯示bitmap圖像。java
AsyncTask的用法還不太清晰,後續添加補答。。。。android
本次任務:安卓客戶端發出請求下載服務器電腦上E盤下的一張名爲u.jpg的圖片。服務器
效果圖:app
package two.com.two; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import two.com.two.R; public class HttpImageDownload extends Activity { //ctrl+alt+L是android studio的代碼排版快速鍵 ImageView iv; Button bon; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.image_download); iv = (ImageView) findViewById(R.id.imageView); bon = (Button) findViewById(R.id.tv_btn); bon.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new thread(). execute("http://ly-and-tl.uicp.cn:42696/AndroidServer/HttpImageDownload?"); } }); } class thread extends AsyncTask<String, Void, Bitmap> { @Override protected void onPostExecute(Bitmap bitmap) { iv.setImageBitmap(bitmap); //此方法在主線程中執行因此能夠更新控件 } @Override protected Bitmap doInBackground(String... arg0) { // TODO Auto-generated method stub Bitmap bit = null; try { URL url = new URL(arg0[0]); HttpURLConnection huc = (HttpURLConnection) url.openConnection(); InputStream is = huc.getInputStream(); //重點了解BitmapFactory這個類 // decodeStream()方法網上說低版本有時會出錯是安卓自己的一個錯誤 //此方法在後臺線程中執行 bit = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bit; } } }
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //servlet3.0後簡單的定義網址的方式 @WebServlet("/HttpImageDownload") public class HttpImageDownload extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String path="E:/u.jpg";//圖片在本機路徑 File file=new File(path); if(file.exists()) System.out.println("存在!"); FileInputStream fis=new FileInputStream(file); String filename=URLEncoder.encode(file.getName(), "UTF-8"); response.setHeader("Content-Disposition", "attachment;"+"filename="+filename);//附件 response.setCharacterEncoding("UTF-8"); ServletOutputStream os=response.getOutputStream(); int len=0; byte[] b=new byte[1024]; while((len=fis.read(b))!=-1) { os.write(b, 0, len); } os.flush();//發送 if(fis!=null)fis.close(); if(os!=null) os.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }