編輯:關於 Android 程式設計
在專案開發過程中,多檔案批次上傳是很常見的需求。最近剛好把這個問題徹底解決,於是整理成文章記錄下來,方便日後隨時查閱參考。
實測發現,某些客戶端與服務端的組合會讓批次上傳無法正常運作:
1. Android 客戶端 + SpringMVC 服務端:若服務端使用 org.springframework.web.multipart.MultipartHttpServletRequest 作為接收類,批次上傳往往會失敗,最後伺服器只收到第一個檔案。推測原因是 MultipartHttpServletRequest 對原生 HttpServletRequest 進行了封裝,導致多檔案解析出現問題。
2. Android 客戶端 + StrutsMVC 服務端:這種組合我沒有親自測試,暫不評價。若有網友採用此法上傳失敗,原因可能與第 1 點相同。
經過驗證,採用 Android 客戶端 + Servlet(HttpServletRequest) 的方式可以穩定完成多檔案上傳。以下分別說明服務端與客戶端實作。
Servlet 端程式碼如下:
DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List items = upload.parseRequest(request); Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { System.out.println("表單參數名:" + item.getFieldName() + ",表單參數值:" + item.getString("UTF-8")); } else { if (item.getName() != null && !item.getName().equals("")) { System.out.println("上傳文件的大小:" + item.getSize()); System.out.println("上傳文件的類型:" + item.getContentType()); // item.getName()返回上傳文件在客戶端的完整路徑名稱 System.out.println("上傳文件的名稱:" + item.getName()); File tempFile = new File(item.getName()); // 上傳文件的保存路徑 File file = new File(sc.getRealPath("/") + savePath, tempFile.getName()); item.write(file); request.setAttribute("upload.message", "上傳文件成功!"); } else { request.setAttribute("upload.message", "沒有選擇上傳文件!"); } } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); request.setAttribute("upload.message", "上傳文件失敗!"); } request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
Android 端程式碼如下:
public class SocketHttpRequester { /** *多文件上傳 * 直接通過HTTP協議提交數據到服務器,實現如下面表單提交功能: * * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用https://www.iteye.cn或https://192.168.1.101:8083這樣的路徑測試) * @param params 請求參數 key為參數名,value為參數值 * @param file 上傳文件 */ public static boolean post(String path, MapFormFile 類的程式碼如下:
public class FormFile { /* 上傳文件的數據 */ private byte[] data; private InputStream inStream; private File file; private int fileSize; /* 文件名稱 */ private String filname; /* 請求參數名稱*/ private String parameterName; /* 內容類型 */ private String contentType = "application/octet-stream"; public FormFile(String filname, byte[] data, String parameterName, String contentType) { this.data = data; this.filname = filname; this.parameterName = parameterName; if(contentType!=null) this.contentType = contentType; } public FormFile(String filname, File file, String parameterName, String contentType) { this.filname = filname; this.parameterName = parameterName; this.file = file; try { this.inStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } if(contentType!=null) this.contentType = contentType; } public FormFile(InputStream inStream, int fileSize, String filname, String parameterName, String contentType) { super(); this.inStream = inStream; this.fileSize = fileSize; this.filname = filname; this.parameterName = parameterName; this.contentType = contentType; } public int getFileSize() { return fileSize; } public File getFile() { return file; } public InputStream getInStream() { return inStream; } public byte[] getData() { return data; } public String getFilname() { return filname; } public void setFilname(String filname) { this.filname = filname; } public String getParameterName() { return parameterName; } public void setParameterName(String parameterName) { this.parameterName = parameterName; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } }
補充說明:以上 Android 端上傳程式碼參考自網路資源,在此向原作者致謝。經實際測試,該程式碼能夠正常完成多檔案上傳。
動手打造帶刻度與文字提示的水平自定義進度條
在實際開發中,系統自帶的 ProgressBar 往往難以滿足個性化設計的需求。本文以一個「帶刻度、並在進度條上顯示數值」的水平進度條為例,完整走一遍自定義 View 的
Android 照片色彩調整:用 ColorMatrix 做即時調色
要讓使用者即時調整照片的紅、綠、藍與整體飽和度,最直覺的做法是四個 SeekBar 搭配 ColorMatrix。 activity_main.xml <Lin
Launcher 開發實戰:桌面快捷方式 ShortCut 的創建機制
以下分析基於 Launcher2.3 的源碼。 一、基礎知識:LAUNCHER 類別 每個應用的主 Activity 通常都會聲明: <category andr
RecyclerView 下拉刷新與上拉加載更多的實現
下拉刷新和上拉加載幾乎是每個列表頁的標配。有意思的是,這兩個功能看似一對,實現方式卻完全不同:一個用官方現成組件,一個需要自己寫監聽器。 效果預覽 下拉刷新 上拉加載更多