Android入門:文件上傳

http://blog.csdn.net/xiazdong/article/category/1106409

文件上傳分爲兩個部分:

(1)服務器端:需要使用FileUpload+common.io實現文件的上傳;

(2)客戶端:需要模擬文件上傳的HTTP請求頭;



一、服務器端代碼


FileServlet.java

  1. package org.xiazdong.servlet;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.List;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.annotation.WebServlet;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12.   
  13. import org.apache.commons.fileupload.FileItem;  
  14. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  15. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  16.   
  17. @WebServlet("/FileServlet")  
  18. public class FileServlet extends HttpServlet {  
  19.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  20.     }  
  21.   
  22.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  23.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  24.         ServletFileUpload upload = new ServletFileUpload(factory);  
  25.         upload.setFileSizeMax(1024*1024);                       //設置上傳文件的最大容量  
  26.         try{  
  27.             List<FileItem>items  = upload.parseRequest(request);  //取得表單全部數據  
  28.             for(FileItem item:items){  
  29.                 if(!item.isFormField()){    //如果是上傳的文件  
  30.                      String name = "D:\\"+item.getName().substring(item.getName().lastIndexOf('\\')+1);      
  31.                      String filename = name;  
  32.                      System.out.println(filename);  
  33.                      File f = new File(filename);   //保存到D盤  
  34.                      item.write(f);  
  35.                      System.out.println("上傳成功");  
  36.                 }  
  37.             }  
  38.         }  
  39.         catch(Exception e){  
  40.             e.printStackTrace();  
  41.         }  
  42.     }  
  43.   
  44. }  

瀏覽器端代碼:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Server Title</title>  
  6. </head>  
  7. <body>  
  8.     <form action="/Server/FileServlet" method="post" enctype="multipart/form-data">  
  9.         文件上傳:<input type="file" name="filename"/><br/>  
  10.         <input type="submit" value="get提交">  
  11.     </form>  
  12. </body>  
  13. </html>  



二、客戶端前期準備及核心代碼


1.前期準備


由於客戶端需要模擬HTTP請求,因此我們可以先來看下文件上傳的HTTP請求:

POST /Server/FileServlet HTTP/1.1
Accept: */*
Referer: http://localhost:8080/Server/2.html
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; .NET4.0C; InfoPath.3)
Content-Type: multipart/form-data; boundary=---------------------------7dc372520758    //此處爲分隔符,用來分隔多個文件和參數
Accept-Encoding: gzip, deflate
Host: localhost:8080
Content-Length: 14610
Connection: Keep-Alive
Cache-Control: no-cache


-----------------------------7dc372520758
Content-Disposition: form-data; name="filename"; filename="D:\lv6.GIF"
Content-Type: image/gif
文件內容
-----------------------------7dc372520758--   //結束時需要多加兩個--

由此看出,這個HTTP請求比較難以模擬,此處封裝了一個輔助類,是黎活明老師實現的,我們可以直接使用:

HttpRequestUtil.uploadFile(String path, Map<String, String> params, FormFile file)

path:URL

params:一般的參數

file:文件

HttpRequestUtil.uploadFiles(String path, Map<String, String> params, FormFile[] files)

path:URL

params:一般的參數

files:多個文件


FormFile.java 

  1. package com.xiazdong.netword.http.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.InputStream;  
  7.   
  8. /** 
  9.  * 上傳文件 
  10.  */  
  11. public class FormFile {  
  12.     /* 上傳文件的數據 */  
  13.     private byte[] data;  
  14.     private InputStream inStream;  
  15.     private File file;  
  16.     /* 文件名稱 */  
  17.     private String filname;  
  18.     /* 請求參數名稱*/  
  19.     private String parameterName;  
  20.     /* 內容類型 */  
  21.     private String contentType = "application/octet-stream";  
  22.       
  23.     /** 
  24.      * 此函數用來傳輸小文件 
  25.      * @param filname 
  26.      * @param data 
  27.      * @param parameterName HTML的控件參數名稱 
  28.      * @param contentType 
  29.      */  
  30.     public FormFile(String filname, byte[] data, String parameterName, String contentType) {  
  31.         this.data = data;  
  32.         this.filname = filname;  
  33.         this.parameterName = parameterName;  
  34.         if(contentType!=nullthis.contentType = contentType;  
  35.     }  
  36.     /** 
  37.      * 此函數用來傳輸大文件 
  38.      * @param filname 
  39.      * @param file 
  40.      * @param parameterName 
  41.      * @param contentType 
  42.      */  
  43.     public FormFile(String filname, File file, String parameterName, String contentType) {  
  44.         this.filname = filname;  
  45.         this.parameterName = parameterName;  
  46.         this.file = file;  
  47.         try {  
  48.             this.inStream = new FileInputStream(file);  
  49.         } catch (FileNotFoundException e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.         if(contentType!=nullthis.contentType = contentType;  
  53.     }  
  54.       
  55.     public File getFile() {  
  56.         return file;  
  57.     }  
  58.   
  59.     public InputStream getInStream() {  
  60.         return inStream;  
  61.     }  
  62.   
  63.     public byte[] getData() {  
  64.         return data;  
  65.     }  
  66.   
  67.     public String getFilname() {  
  68.         return filname;  
  69.     }  
  70.   
  71.     public void setFilname(String filname) {  
  72.         this.filname = filname;  
  73.     }  
  74.   
  75.     public String getParameterName() {  
  76.         return parameterName;  
  77.     }  
  78.   
  79.     public void setParameterName(String parameterName) {  
  80.         this.parameterName = parameterName;  
  81.     }  
  82.   
  83.     public String getContentType() {  
  84.         return contentType;  
  85.     }  
  86.   
  87.     public void setContentType(String contentType) {  
  88.         this.contentType = contentType;  
  89.     }  
  90.       
  91. }  


HttpRequestUtil.java


  1. package com.xiazdong.netword.http.util;  
  2.   
  3.   
  4. import java.io.BufferedReader;  
  5. import java.io.ByteArrayOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.io.OutputStream;  
  9. import java.net.HttpURLConnection;  
  10. import java.net.InetAddress;  
  11. import java.net.Socket;  
  12. import java.net.URL;  
  13. import java.net.URLConnection;  
  14. import java.net.URLEncoder;  
  15. import java.util.HashMap;  
  16. import java.util.Map;  
  17. import java.util.Map.Entry;  
  18. import java.util.Set;  
  19.   
  20. /* 
  21.  * 此類用來發送HTTP請求 
  22.  * */  
  23. public class HttpRequestUtil {  
  24.     /** 
  25.      * 直接通過HTTP協議提交數據到服務器,實現如下面表單提交功能: 
  26.      *   <FORM METHOD=POST ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data"> 
  27.             <INPUT TYPE="text" NAME="name"> 
  28.             <INPUT TYPE="text" NAME="id"> 
  29.             <input type="file" name="imagefile"/> 
  30.             <input type="file" name="zip"/> 
  31.          </FORM> 
  32.      * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因爲它會指向手機模擬器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080這樣的路徑測試) 
  33.      * @param params 請求參數 key爲參數名,value爲參數值 
  34.      * @param file 上傳文件 
  35.      */  
  36.     public static boolean uploadFiles(String path, Map<String, String> params, FormFile[] files) throws Exception{       
  37.         final String BOUNDARY = "---------------------------7da2137580612"//數據分隔線  
  38.         final String endline = "--" + BOUNDARY + "--\r\n";//數據結束標誌  
  39.           
  40.         int fileDataLength = 0;  
  41.         if(files!=null&&files.length!=0){  
  42.             for(FormFile uploadFile : files){//得到文件類型數據的總長度  
  43.                 StringBuilder fileExplain = new StringBuilder();  
  44.                 fileExplain.append("--");  
  45.                 fileExplain.append(BOUNDARY);  
  46.                 fileExplain.append("\r\n");  
  47.                 fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");  
  48.                 fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");  
  49.                 fileExplain.append("\r\n");  
  50.                 fileDataLength += fileExplain.length();  
  51.                 if(uploadFile.getInStream()!=null){  
  52.                     fileDataLength += uploadFile.getFile().length();  
  53.                 }else{  
  54.                     fileDataLength += uploadFile.getData().length;  
  55.                 }  
  56.             }  
  57.         }  
  58.         StringBuilder textEntity = new StringBuilder();  
  59.         if(params!=null&&!params.isEmpty()){  
  60.             for (Map.Entry<String, String> entry : params.entrySet()) {//構造文本類型參數的實體數據  
  61.                 textEntity.append("--");  
  62.                 textEntity.append(BOUNDARY);  
  63.                 textEntity.append("\r\n");  
  64.                 textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");  
  65.                 textEntity.append(entry.getValue());  
  66.                 textEntity.append("\r\n");  
  67.             }  
  68.         }  
  69.         //計算傳輸給服務器的實體數據總長度  
  70.         int dataLength = textEntity.toString().getBytes().length + fileDataLength +  endline.getBytes().length;  
  71.           
  72.         URL url = new URL(path);  
  73.         int port = url.getPort()==-1 ? 80 : url.getPort();  
  74.         Socket socket = new Socket(InetAddress.getByName(url.getHost()), port);          
  75.         OutputStream outStream = socket.getOutputStream();  
  76.         //下面完成HTTP請求頭的發送  
  77.         String requestmethod = "POST "+ url.getPath()+" HTTP/1.1\r\n";  
  78.         outStream.write(requestmethod.getBytes());  
  79.         String accept = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";  
  80.         outStream.write(accept.getBytes());  
  81.         String language = "Accept-Language: zh-CN\r\n";  
  82.         outStream.write(language.getBytes());  
  83.         String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "\r\n";  
  84.         outStream.write(contenttype.getBytes());  
  85.         String contentlength = "Content-Length: "+ dataLength + "\r\n";  
  86.         outStream.write(contentlength.getBytes());  
  87.         String alive = "Connection: Keep-Alive\r\n";  
  88.         outStream.write(alive.getBytes());  
  89.         String host = "Host: "+ url.getHost() +":"+ port +"\r\n";  
  90.         outStream.write(host.getBytes());  
  91.         //寫完HTTP請求頭後根據HTTP協議再寫一個回車換行  
  92.         outStream.write("\r\n".getBytes());  
  93.         //把所有文本類型的實體數據發送出來  
  94.         outStream.write(textEntity.toString().getBytes());           
  95.         //把所有文件類型的實體數據發送出來  
  96.         if(files!=null&&files.length!=0){  
  97.             for(FormFile uploadFile : files){  
  98.                 StringBuilder fileEntity = new StringBuilder();  
  99.                 fileEntity.append("--");  
  100.                 fileEntity.append(BOUNDARY);  
  101.                 fileEntity.append("\r\n");  
  102.                 fileEntity.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");  
  103.                 fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");  
  104.                 outStream.write(fileEntity.toString().getBytes());  
  105.                 if(uploadFile.getInStream()!=null){  
  106.                     byte[] buffer = new byte[1024];  
  107.                     int len = 0;  
  108.                     while((len = uploadFile.getInStream().read(buffer, 01024))!=-1){  
  109.                         outStream.write(buffer, 0, len);  
  110.                     }  
  111.                     uploadFile.getInStream().close();  
  112.                 }else{  
  113.                     outStream.write(uploadFile.getData(), 0, uploadFile.getData().length);  
  114.                 }  
  115.                 outStream.write("\r\n".getBytes());  
  116.             }  
  117.         }  
  118.         //下面發送數據結束標誌,表示數據已經結束  
  119.         outStream.write(endline.getBytes());  
  120.           
  121.         BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  122.         if(reader.readLine().indexOf("200")==-1){//讀取web服務器返回的數據,判斷請求碼是否爲200,如果不是200,代表請求失敗  
  123.             return false;  
  124.         }  
  125.         outStream.flush();  
  126.         outStream.close();  
  127.         reader.close();  
  128.         socket.close();  
  129.         return true;  
  130.     }  
  131.     /**  
  132.      * 提交數據到服務器  
  133.      * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因爲它會指向手機模擬器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080這樣的路徑測試)  
  134.      * @param params 請求參數 key爲參數名,value爲參數值  
  135.      * @param file 上傳文件  
  136.      */  
  137.     public static boolean uploadFile(String path, Map<String, String> params, FormFile file) throws Exception{  
  138.        return uploadFiles(path, params, new FormFile[]{file});  
  139.     }  
  140.     /** 
  141.      * 將輸入流轉爲字節數組 
  142.      * @param inStream 
  143.      * @return 
  144.      * @throws Exception 
  145.      */  
  146.     public static byte[] read2Byte(InputStream inStream)throws Exception{  
  147.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  148.         byte[] buffer = new byte[1024];  
  149.         int len = 0;  
  150.         while( (len = inStream.read(buffer)) !=-1 ){  
  151.             outSteam.write(buffer, 0, len);  
  152.         }  
  153.         outSteam.close();  
  154.         inStream.close();  
  155.         return outSteam.toByteArray();  
  156.     }  
  157.     /** 
  158.      * 將輸入流轉爲字符串 
  159.      * @param inStream 
  160.      * @return 
  161.      * @throws Exception 
  162.      */  
  163.     public static String read2String(InputStream inStream)throws Exception{  
  164.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  165.         byte[] buffer = new byte[1024];  
  166.         int len = 0;  
  167.         while( (len = inStream.read(buffer)) !=-1 ){  
  168.             outSteam.write(buffer, 0, len);  
  169.         }  
  170.         outSteam.close();  
  171.         inStream.close();  
  172.         return new String(outSteam.toByteArray(),"UTF-8");  
  173.     }  
  174. }  



2.核心代碼


  1. FormFile formFile = new FormFile(file.getName(), file, "document""text/plain");//"document"爲控件的名稱,"text/plain"爲文件的mimetype  
  2. boolean isSuccess = HttpRequestUtil.uploadFile("http://192.168.0.103:8080/Server/FileServlet"null, formFile);  


三、客戶端代碼







AndroidManifest.xml

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  3. <uses-permission android:name="android.permission.INTERNET"/>  



MainActivity.java

  1. package org.xiazdong.network.fileupload;  
  2.   
  3. import java.io.File;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.os.Environment;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13.   
  14. import com.xiazdong.netword.http.util.FormFile;  
  15. import com.xiazdong.netword.http.util.HttpRequestUtil;  
  16.   
  17. public class MainActivity extends Activity {  
  18.     private EditText fileName;  
  19.     private Button button;  
  20.     private OnClickListener listener = new OnClickListener(){  
  21.         @Override  
  22.         public void onClick(View v) {  
  23.             String fname = fileName.getText().toString();  
  24.             if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)||Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)){  
  25.                 File file = new File(Environment.getExternalStorageDirectory(),fname);  //獲得SDCARD的文件  
  26.                 if(file.exists()){  
  27.                     FormFile formFile = new FormFile(file.getName(), file, "document""text/plain");  
  28.                     try {  
  29.                         boolean isSuccess = HttpRequestUtil.uploadFile("http://192.168.0.103:8080/Server/FileServlet"null, formFile);  
  30.                         if(isSuccess){  
  31.                             Toast.makeText(MainActivity.this"文件上傳成功", Toast.LENGTH_SHORT).show();  
  32.                         }  
  33.                         else{  
  34.                             Toast.makeText(MainActivity.this"文件上傳失敗", Toast.LENGTH_SHORT).show();  
  35.                         }  
  36.                     } catch (Exception e) {  
  37.                         e.printStackTrace();  
  38.                     }  
  39.                 }  
  40.                 else{  
  41.                     Toast.makeText(MainActivity.this"文件不存在", Toast.LENGTH_SHORT).show();  
  42.                 }  
  43.             }  
  44.             else{  
  45.                 Toast.makeText(MainActivity.this"SDCARD不存在", Toast.LENGTH_SHORT).show();  
  46.             }  
  47.         }  
  48.     };  
  49.     @Override  
  50.     public void onCreate(Bundle savedInstanceState) {  
  51.         super.onCreate(savedInstanceState);  
  52.         setContentView(R.layout.main);  
  53.         fileName = (EditText)this.findViewById(R.id.filename);  
  54.         button = (Button)this.findViewById(R.id.button);  
  55.         button.setOnClickListener(listener);  
  56.     }  

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