首先,以下架構下的批量文件上傳可能會失敗或者不會成功:

員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團隊的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。創(chuàng)新互聯(lián)公司堅持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因為“專注所以專業(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供成都網(wǎng)站設計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設、微信公眾號開發(fā)、電商網(wǎng)站開發(fā),微信小程序開發(fā),軟件定制開發(fā)等一站式互聯(lián)網(wǎng)企業(yè)服務。
1.android客戶端+springMVC服務端:服務端采用org.springframework.web.multipart.MultipartHttpServletRequest作為批量上傳接收類,這種搭配下的批量文件上傳會失敗,最終服務端只會接受到一個文件,即只會接受到第一個文件。可能因為MultipartHttpServletRequest對servlet原本的HttpServletRequest類進行封裝,導致批量上傳有問題。
2.android客戶端+strutsMVC服務端:
上傳成功的方案:
采用android客戶端+Servlet(HttpServletRequest)進行文件上傳。
Servlet端代碼如下:
[java] view plaincopyprint?
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("表單參數(shù)名:" + item.getFieldName() + ",表單參數(shù)值:" + 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端代碼如下:
[java] view plaincopyprint?
public class SocketHttpRequester {
/**
*多文件上傳
* 直接通過HTTP協(xié)議提交數(shù)據(jù)到服務器,實現(xiàn)如下面表單提交功能:
* FORM METHOD=POST ACTION="" enctype="multipart/form-data"
INPUT TYPE="text" NAME="name"
INPUT TYPE="text" NAME="id"
input type="file" name="imagefile"/
input type="file" name="zip"/
/FORM
* @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用或這樣的路徑測試)
* @param params 請求參數(shù) key為參數(shù)名,value為參數(shù)值
* @param file 上傳文件
*/
public static boolean post(String path, MapString, String params, FormFile[] files) throws Exception{
final String BOUNDARY = "---------------------------7da2137580612"; //數(shù)據(jù)分隔線
final String endline = "--" + BOUNDARY + "--\r\n";//數(shù)據(jù)結束標志
int fileDataLength = 0;
for(FormFile uploadFile : files){//得到文件類型數(shù)據(jù)的總長度
StringBuilder fileExplain = new StringBuilder();
fileExplain.append("--");
fileExplain.append(BOUNDARY);
fileExplain.append("\r\n");
fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");
fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");
fileExplain.append("\r\n");
fileDataLength += fileExplain.length();
if(uploadFile.getInStream()!=null){
fileDataLength += uploadFile.getFile().length();
}else{
fileDataLength += uploadFile.getData().length;
}
}
StringBuilder textEntity = new StringBuilder();
for (Map.EntryString, String entry : params.entrySet()) {//構造文本類型參數(shù)的實體數(shù)據(jù)
textEntity.append("--");
textEntity.append(BOUNDARY);
textEntity.append("\r\n");
textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
textEntity.append(entry.getValue());
textEntity.append("\r\n");
}
//計算傳輸給服務器的實體數(shù)據(jù)總長度
int dataLength = textEntity.toString().getBytes().length + fileDataLength + endline.getBytes().length;
URL url = new URL(path);
int port = url.getPort()==-1 ? 80 : url.getPort();
Socket socket = new Socket(InetAddress.getByName(url.getHost()), port);
OutputStream outStream = socket.getOutputStream();
//下面完成HTTP請求頭的發(fā)送
String requestmethod = "POST "+ url.getPath()+" HTTP/1.1\r\n";
outStream.write(requestmethod.getBytes());
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";
outStream.write(accept.getBytes());
String language = "Accept-Language: zh-CN\r\n";
outStream.write(language.getBytes());
String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "\r\n";
outStream.write(contenttype.getBytes());
String contentlength = "Content-Length: "+ dataLength + "\r\n";
outStream.write(contentlength.getBytes());
String alive = "Connection: Keep-Alive\r\n";
outStream.write(alive.getBytes());
String host = "Host: "+ url.getHost() +":"+ port +"\r\n";
outStream.write(host.getBytes());
//寫完HTTP請求頭后根據(jù)HTTP協(xié)議再寫一個回車換行
outStream.write("\r\n".getBytes());
//把所有文本類型的實體數(shù)據(jù)發(fā)送出來
outStream.write(textEntity.toString().getBytes());
//把所有文件類型的實體數(shù)據(jù)發(fā)送出來
for(FormFile uploadFile : files){
StringBuilder fileEntity = new StringBuilder();
fileEntity.append("--");
fileEntity.append(BOUNDARY);
fileEntity.append("\r\n");
fileEntity.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");
fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");
outStream.write(fileEntity.toString().getBytes());
if(uploadFile.getInStream()!=null){
byte[] buffer = new byte[1024];
int len = 0;
while((len = uploadFile.getInStream().read(buffer, 0, 1024))!=-1){
outStream.write(buffer, 0, len);
}
uploadFile.getInStream().close();
}else{
outStream.write(uploadFile.getData(), 0, uploadFile.getData().length);
}
outStream.write("\r\n".getBytes());
}
//下面發(fā)送數(shù)據(jù)結束標志,表示數(shù)據(jù)已經(jīng)結束
outStream.write(endline.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(reader.readLine().indexOf("200")==-1){//讀取web服務器返回的數(shù)據(jù),判斷請求碼是否為200,如果不是200,代表請求失敗
return false;
}
outStream.flush();
outStream.close();
reader.close();
socket.close();
return true;
}
/**
*單文件上傳
* 提交數(shù)據(jù)到服務器
* @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用或這樣的路徑測試)
* @param params 請求參數(shù) key為參數(shù)名,value為參數(shù)值
* @param file 上傳文件
*/
public static boolean post(String path, MapString, String params, FormFile file) throws Exception{
return post(path, params, new FormFile[]{file});
}
}
(1)登錄Android客戶端,在主界面下方點擊“上傳”按鈕,(2)在SD卡中選擇文件,點擊“確定”按鈕,文件即進入傳輸列表進行上傳。了解更多服務優(yōu)惠點擊下方的“官方網(wǎng)址”客服221為你解答。
具體方法如下:
1、首先打開電腦上的qq,在首界面找到我的設備。
同時打開手機qq,并連上無線。
2、在打開了我的設備后,在我的設備下,點擊左下角的文件傳輸標志。
3、尋找在瀏覽器中提前下載好的apk文件。
4、打開后,自動上傳到手機上。
我是這樣做的
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "請選擇一個要上傳的文件"), 1);
然后選擇文件后調(diào)用
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String url= uri.toString();
}
}
獲得路徑,根據(jù)路徑調(diào)用
public String convertCodeAndGetText(String str_filepath) {// 轉(zhuǎn)碼\
try {
File file1 = new File(str_filepath);
file_name = file1.getName();
FileInputStream in = new FileInputStream(file1);
byte[] buffer = new byte[(int) file1.length() + 100];
int length = in.read(buffer);
load = Base64.encodeToString(buffer, 0, length,
Base64.DEFAULT);
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return load;
}
對文件進行編碼
新聞名稱:android上傳,android上傳文件到服務器
本文路徑:http://chinadenli.net/article31/dsiejsd.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、域名注冊、網(wǎng)站改版、微信公眾號、定制網(wǎng)站、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)