㈠ android-async-http能上傳多個文件嗎
我這有個httpclient寫的工具類
批量上傳的話,只需要傳遞一個FIle對象還可以帶上傳進度條
服務端的話就簡單了
如果是struts的話,只要定義一個fileUpload的攔截器,定義一個List<File> 對象就會自動注入了
如果是servlet的話,那就要用FileUpload + io 兩個架包,自己寫代碼去讀取文件了
我這有個客戶端,android寫的
㈡ android如何批量上傳大量圖片急啊
lz,有沒有解決?還望給出答案!
㈢ 如何上傳多個文件asynchttpclient Android問題,怎麼解決
直接實例化多個file傳到params裡面就可以了
RequestParams params = new RequestParams();
for (int i = 0; i < list.size(); i++) {
try {
params.put("images[" + i + "]", new File(list.get(i)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
㈣ android 文件流的方式多張圖片上傳,並多個參數
android 開發中圖片上傳是很正常的,有兩種可用的方式:
下面我們就說明一下以文件流上傳圖片的方式, 實現網路框架是Retrofit
測試上傳3張手機sd卡中的圖片,並傳人了參數EquipmentCode, Description, ReportUserCode等
其中的思路是: Post的方式,Content-Type:multipart/form-data的類型進行上傳文件的。
其中MultipartBody是RequestBody的擴展,
看看請求頭的信息, 請求中攜帶了所有信(如果介面開發人員說不能收到, 叫他自己想想,截圖給他,哈哈哈:)
上面的是上傳了3張圖片,如果一張,只要傳一個就行!
就這樣,圖片上傳的兩種方式ok拉,測試通過的,保證正確!
參考: https://www.jianshu.com/p/acfefb0a204f
㈤ android上大文件分片上傳 具體怎麼弄
提供一點demo
斷點續傳(改良版)
package com.phone1000.demo09;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessDemo {
public static void main(String[] args) {
// 1.找到文件
File file = new File("E:\\網路雲盤\\網路雲同步盤\\Android開發視頻教程\\[Android開發視頻教程]02_01_spinner的使用.mp4");
File file2 = new File("E:\\我的照片\\[Android開發視頻教程]02_01_spinner的使用.mp4");
//2.創建流
RandomAccessFile is = null;
FileOutputStream os = null;
try {
is = new RandomAccessFile(file,"r");
os = new FileOutputStream(file2,true);
//3.定義一個容器
byte[] b = new byte[1024];
//4.定義一個長度
int len = 0 ;
long oldLength = file.length();
long newLength = 0;
//5.循環讀數
while((len = is.read(b)) != -1){
if(newLength >= oldLength)
{
System.out.println("傳輸完成!");
break;
}
else{
newLength = newLength + len;
is.seek(newLength);
os.write(b);
}
}//釋放資源
os.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
㈥ android 怎麼多圖上傳 okhttp
android上傳圖片是先將圖片文件轉換成流文件:可用以下代碼轉換流文件,imgPath為圖片的完整地址
//圖片轉化成base64字元串
public static String imgToBase64(String imgPath) {
InputStream in = null;
byte[] data = null;
//讀取圖片位元組數組
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e){
e.printStackTrace();
}
//對位元組數組Base64編碼
sun.misc.BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64編碼過的位元組數組字元串
}
然後圖片文件就成為一串字元串啦,傳遞方法和普通字元串一樣,多圖使用分號隔開即可,後台收到後直接將流文件轉換成圖片保存即可。
㈦ android如何實現圖片批量上傳
首先,以下架構下的批量文件上傳可能會失敗或者不會成功:
1.android客戶端+springMVC服務端:服務端採用org.springframework.web.multipart.MultipartHttpServletRequest作為批量上傳接收類,這種搭配下的批量文件上傳會失敗,最終服務端只會接受到一個文件,即只會接受到第一個文件。可能因為MultipartHttpServletRequest對servlet原本的HttpServletRequest類進行封裝,導致批量上傳有問題。
2.android客戶端+strutsMVC服務端:
上傳成功的方案:
採用android客戶端+Servlet(HttpServletRequest)進行文件上傳。
Servlet端代碼如下:
[java] view plainprint?
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端代碼如下:
[java] view plainprint?
public class SocketHttpRequester {
/**
*多文件上傳
* 直接通過HTTP協議提交數據到伺服器,實現如下面表單提交功能:
* <FORM METHOD=POST ACTION="http://192.168.1.101:8083/upload/servlet/UploadServlet" 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這樣的路徑測試,因為它會指向手機模擬器,你可以使用http://www.iteye.cn或http://192.168.1.101:8083這樣的路徑測試)
* @param params 請求參數 key為參數名,value為參數值
* @param file 上傳文件
*/
public static boolean post(String path, Map<String, String> params, FormFile[] files) throws Exception{
final String BOUNDARY = "---------------------------7da2137580612"; //數據分隔線
final String endline = "--" + BOUNDARY + "--\r\n";//數據結束標志
int fileDataLength = 0;
for(FormFile uploadFile : files){//得到文件類型數據的總長度
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.Entry<String, String> entry : params.entrySet()) {//構造文本類型參數的實體數據
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");
}
//計算傳輸給伺服器的實體數據總長度
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請求頭的發送
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請求頭後根據HTTP協議再寫一個回車換行
outStream.write("\r\n".getBytes());
//把所有文本類型的實體數據發送出來
outStream.write(textEntity.toString().getBytes());
//把所有文件類型的實體數據發送出來
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());
}
//下面發送數據結束標志,表示數據已經結束
outStream.write(endline.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(reader.readLine().indexOf("200")==-1){//讀取web伺服器返回的數據,判斷請求碼是否為200,如果不是200,代表請求失敗
return false;
}
outStream.flush();
outStream.close();
reader.close();
socket.close();
return true;
}
/**
*單文件上傳
* 提交數據到伺服器
* @param path 上傳路徑(註:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080這樣的路徑測試)
* @param params 請求參數 key為參數名,value為參數值
* @param file 上傳文件
*/
public static boolean post(String path, Map<String, String> params, FormFile file) throws Exception{
return post(path, params, new FormFile[]{file});
}
}
㈧ 手機是安卓Android手機,天翼雲存儲上傳文件怎麼操作
天翼雲存儲上傳文件具體操作如下:1、通過網頁版上傳文件:1)登錄天翼雲存儲,在頁面上方點擊「上傳文件」按鈕;2)在彈出的對話框選擇「添加文件」按鈕;3)在彈出文件框選擇文件,點擊「打開」按鈕即可上傳。2、通過PC客戶端上傳文件:登錄PC客戶端,把需要上傳的文件直接拖動到PC客戶端即可進行上傳。3、通過Android客戶端上傳文件:1)登錄Android客戶端,在主界面下方點擊「上傳」按鈕,2)在SD卡中選擇文件,點擊「確定」按鈕,文件即進入傳輸列表進行上傳。4、通過iPhone或iPad客戶端上傳文件跟Android客戶端方法基本一致,但iPhone、iPad沒有SD卡,需在「照片」或「視頻」中選擇文件上傳。「天翼雲存儲」是為有天翼雲存儲需求的天翼帳號用戶提供的安全、高速、大容量的在線存儲管理、備份及共享的服務,支持多終端的個人數據中心,包括了媒體自動篩選、文件在線瀏覽及播放、雲轉碼瀏覽及播放、通訊錄安全備份、同步備份二合一等功能。無固定有效期,業務提供方中國電信有終止本業務的權利。適用於有天翼雲存儲需求的天翼帳號用戶。貴州地區用戶關注中國電信貴州客服公眾號可微信繳費,一鍵查話費充值,流量、積分、賬單、詳單均可自助操作,方便快捷。客服233為你解答。
㈨ Android上大文件分片上傳 具體怎麼弄
正常情況下,一般都是在長傳完成後,在伺服器直接保存。
?
1
2
3
4
5
6
7
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//保存文件
context.Request.Files[0].SaveAs(context.Server.MapPath("~/1/" + context.Request.Files[0].FileName));
context.Response.Write("Hello World");
}
最近項目中用網路開源的上傳組件webuploader,官方介紹webuploader支持分片上傳。具體webuploader的使用方法見官網http://fex..com/webuploader/。
?
1
2
3
4
5
6
7
8
9
10
11
12
var uploader = WebUploader.create({
auto: true,
swf:'/webuploader/Uploader.swf',
// 文件接收服務端。
server: '/Uploader.ashx',
// 內部根據當前運行是創建,可能是input元素,也可能是flash.
pick: '#filePicker',
chunked: true,//開啟分片上傳
threads: 1,//上傳並發數
//由於Http的無狀態特徵,在往伺服器發送數據過程傳遞一個進入當前頁面是生成的GUID作為標示
formData: {guid:"<%=Guid.NewGuid().ToString()%>"}
});
webuploader的分片上傳是把文件分成若干份,然後向你定義的文件接收端post數據,如果上傳的文件大於分片的尺寸,就會進行分片,然後會在post的數據中添加兩個form元素chunk和chunks,前者標示當前分片在上傳分片中的順序(從0開始),後者代表總分片數。