首先,以下架構(gòu)下的批量文件上傳可能會(huì)失敗或者不會(huì)成功:
員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團(tuán)隊(duì)的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。創(chuàng)新互聯(lián)公司堅(jiān)持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因?yàn)椤皩W⑺詫I(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、電商網(wǎng)站開發(fā),微信小程序開發(fā),軟件定制開發(fā)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。
1.android客戶端+springMVC服務(wù)端:服務(wù)端采用org.springframework.web.multipart.MultipartHttpServletRequest作為批量上傳接收類,這種搭配下的批量文件上傳會(huì)失敗,最終服務(wù)端只會(huì)接受到一個(gè)文件,即只會(huì)接受到第一個(gè)文件??赡芤?yàn)镸ultipartHttpServletRequest對(duì)servlet原本的HttpServletRequest類進(jìn)行封裝,導(dǎo)致批量上傳有問題。
2.android客戶端+strutsMVC服務(wù)端:
上傳成功的方案:
采用android客戶端+Servlet(HttpServletRequest)進(jìn)行文件上傳。
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ù)到服務(wù)器,實(shí)現(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這樣的路徑測試,因?yàn)樗鼤?huì)指向手機(jī)模擬器,你可以使用或這樣的路徑測試)
* @param params 請(qǐng)求參數(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ù)結(jié)束標(biāo)志
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()) {//構(gòu)造文本類型參數(shù)的實(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");
}
//計(jì)算傳輸給服務(wù)器的實(shí)體數(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請(qǐng)求頭的發(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請(qǐng)求頭后根據(jù)HTTP協(xié)議再寫一個(gè)回車換行
outStream.write("\r\n".getBytes());
//把所有文本類型的實(shí)體數(shù)據(jù)發(fā)送出來
outStream.write(textEntity.toString().getBytes());
//把所有文件類型的實(shí)體數(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ù)結(jié)束標(biāo)志,表示數(shù)據(jù)已經(jīng)結(jié)束
outStream.write(endline.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(reader.readLine().indexOf("200")==-1){//讀取web服務(wù)器返回的數(shù)據(jù),判斷請(qǐng)求碼是否為200,如果不是200,代表請(qǐng)求失敗
return false;
}
outStream.flush();
outStream.close();
reader.close();
socket.close();
return true;
}
/**
*單文件上傳
* 提交數(shù)據(jù)到服務(wù)器
* @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因?yàn)樗鼤?huì)指向手機(jī)模擬器,你可以使用或這樣的路徑測試)
* @param params 請(qǐng)求參數(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客戶端,在主界面下方點(diǎn)擊“上傳”按鈕,(2)在SD卡中選擇文件,點(diǎn)擊“確定”按鈕,文件即進(jìn)入傳輸列表進(jìn)行上傳。了解更多服務(wù)優(yōu)惠點(diǎn)擊下方的“官方網(wǎng)址”客服221為你解答。
具體方法如下:
1、首先打開電腦上的qq,在首界面找到我的設(shè)備。
同時(shí)打開手機(jī)qq,并連上無線。
2、在打開了我的設(shè)備后,在我的設(shè)備下,點(diǎn)擊左下角的文件傳輸標(biāo)志。
3、尋找在瀏覽器中提前下載好的apk文件。
4、打開后,自動(dòng)上傳到手機(jī)上。
我是這樣做的
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "請(qǐng)選擇一個(gè)要上傳的文件"), 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;
}
對(duì)文件進(jìn)行編碼