當前位置:首頁 » 文件管理 » 安卓文件上傳

安卓文件上傳

發布時間: 2022-02-06 02:54:12

A. 怎麼把電腦上的文件傳到安卓手機

可以使用數據線連接電腦直接拷入手機上,也可使用網路傳輸,如登陸qq、微信,使用文件傳輸助手即可。

B. android端 file文件上傳

我們做web開發的時候幾乎都是通過一個表單來實現上傳。並且是post的方式。而且都必須要加個參數enctype = "multipart/form-data".然後再上傳後台用各種框架里的插件之類的就可以接收了,並沒有關心過這個文件具體是怎麼傳的。現在用android開發 沒有那些框架了,所以不得不關心一下了。

其實我們這種前後台的交互是用的HTTP協議。而http協議默認是傳的字元串。所以我們上傳文件的話要加enctype = "multipart/form-data"這個參數來說明我們這傳的是文件不是字元串了。而我們做web開發的時候,瀏覽器是自動解析HTTP協議的。裡面傳的哪些東西我們不用管。只要記住幾個參數就行。而我們要上傳的文件報文是保存在請求的頭文件裡面的。下面就是上傳文件頭文件的格式:

POST/logsys/home/uploadIspeedLog!doDefault.html HTTP/1.1
Accept: text/plain, */*
Accept-Language: zh-cn
Host: 192.168.24.56
Content-Type:multipart/form-data;boundary=-----------------------------7db372eb000e2
User-Agent: WinHttpClient
Content-Length: 3693
Connection: Keep-Alive
-------------------------------7db372eb000e2
Content-Disposition: form-data; name="file"; filename="kn.jpg"
Content-Type: image/jpeg
(此處省略jpeg文件二進制數據...)
-------------------------------7db372eb000e2--
這就是Http上傳發送的文件格式。而我們要發送的時候必然要遵循這種格式來並且不能出一點差錯包括每行後面的回車,下面一段文字是網上找的感覺寫的比較精彩。(尊重原創:原文地址)

紅色字體部分就是協議的頭。給伺服器上傳數據時,並非協議頭每個欄位都得說明,其中,content-type是必須的,它包括一個類似標志性質的名為boundary的標志,它可以是隨便輸入的字元串。對後面的具體內容也是必須的。它用來分辨一段內容的開始。Content-Length: 3693 ,這里的3693是要上傳文件的總長度。綠色字體部分就是需要上傳的數據,可以是文本,也可以是圖片等。數據內容前面需要有Content-Disposition, Content-Type以及Content-Transfer-Encoding等說明欄位。最後的紫色部分就是協議的結尾了。

注意這一行:

Content-Type: multipart/form-data; boundary=---------------------------7db372eb000e2

根據 rfc1867, multipart/form-data是必須的.

---------------------------7db372eb000e2 是分隔符,分隔多個文件、表單項。其中b372eb000e2 是即時生成的一個數字,用以確保整個分隔符不會在文件或表單項的內容中出現。Form每個部分用分隔符分割,分隔符之前必須加上"--"著兩個字元(即--{boundary})才能被http協議認為是Form的分隔符,表示結束的話用在正確的分隔符後面添加"--"表示結束。

前面的 ---------------------------7d 是 IE 特有的標志,Mozila 為---------------------------71.

每個分隔的數據的都可以用Content-Type來表示下面數據的類型,可以參考rfc1341

C. android實現文件上傳的功能

我是這樣做的
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "請選擇一個要上傳的文件"), 1);
然後選擇文件後調用
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String url= uri.toString();


獲得路徑,根據路徑調用
public String convertCodeAndGetText(String str_filepath) {// 轉碼\
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;
}
對文件進行編碼

D. android okhttp3.0文件上傳是用什麼方式上傳的

手機與電腦連接打91助手點擊文件管理找存儲卡直接拷貝行

E. 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開始),後者代表總分片數。

F. android 怎樣上傳文件至SharePoint

$File = "d:\xxx\backup\url.txt"

$backPath = "d:\xx\backup\"

$errorPath = $backPath+"error.txt"

$importListPath = $backPath+"importList.txt"

$enableBackuo = $true

$enableDelete = $false Write-Host "文件備份:" -NoNewline

if($enableBackuo) {Write-Host "允許" -ForegroundColor green }

else {Write-Host "不允許" -ForegroundColor red}

Write-Host "文件刪除:" -NoNewline

if($enableDelete) {Write-Host "允許" -ForegroundColor green}

else {Write-Host "不允許" -ForegroundColor red}

Write-Host
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
-ForegroundColor green

#下載頁面的全部信息包含完整的html結構

function WebClientDownload($link, $dir){

$Username = "domain\user"

$Password = "password"

$WebClient = New-Object System.Net.WebClient

$WebClient.Credentials = New-Object System.Net.Networkcredential($Username,
$Password)

$WebClient.DownloadFile( $link, $dir )

}

#下載頁面的SharePoint信息,和ribbon菜單中的『另存為副本』效果一樣

function DownLoadPages($file,$dir){

#Downloading file

$binary = $file.OpenBinary()

$stream = New-Object System.IO.FileStream($dir), Create

$writer = New-Object System.IO.BinaryWriter($stream)

$writer.write($binary)

$writer.Close()

}

#正則過濾aspx頁面

function GetMatches($link){

$reg = "(?http://[^/]+/.*)Pages(?

.*\/)(?.*)\.aspx?(?.*$)"

if($link -match $reg){

return $Matches

}

return $null

}

Get-Content $File | foreach-Object {

$regResult = GetMatches($_)

$webUrl = $regResult.web

if($regResult -eq $null){

Write-Host "$($_) --> 失敗,文件格式不對" -ForegroundColor red

Out-File -FilePath $errorPath -InputObject "$($_) --無法處理" -Append

}

else{

$web = Get-SPWeb $webUrl

$list = $web.lists["頁面"]

#設置導出文件的路徑

$itemUrl = $regResult.page+".aspx"

$folderUrl = $regResult.dir

$fileUrl = ("pages",$folderUrl,$itemUrl -Join "")

$directory = ($backPath,$web.Title.ToString(),$folderUrl -Join "")

$outFileUrl = ($directory,$itemUrl -Join "\")

$spFolder= $web.GetFolder($list.RootFolder.Url + $folderUrl)

#$Items = $list.Items | where {$_['FileLeafRef'] -eq $itemUrl}

try{

$Items = $spFolder.Files.Item($itemUrl)

if($Items){

#判斷目錄是否存在

if(!(Test-Path $directory)){

New-Item -ItemType Directory -Path $directory

#new-item -path $backPath -name $web.Title.ToString() -type directory

}

DownLoadPages $web.GetFile($fileUrl) $outFileUrl

Out-File -FilePath $importListPath -InputObject
"$($_,$outFileUrl,$folderUrl,$Items.Title -Join ',')" -Append

WebClientDownload $regResult[0]
$outFileUrl.Replace(".aspx","_WebClientDownload.aspx")

if($enableDelete){

$Items.Recycle()

}

Write-Host "$($_) --> 成功" -ForegroundColor green

}

else{

Write-Host "$($_) --> 失敗,文件不存在" -ForegroundColor red

#如果URL不存在或已刪除,則輸出提示

Out-File -FilePath $errorPath -InputObject "$($_,'not exist' -Join '--')"
-Append

}

}

catch{

Write-Host "$($regResult[0]) --> ERROR" -ForegroundColor red

Out-File -FilePath $errorPath -InputObject "$($_.exception) -- $fileUrl"
-Append

}

finally{

$web.dispose()

}

}

}

#去除重復行

$output_importlist = get-content $importListPath | sort | get-unique

Out-File -FilePath $importListPath -InputObject $output_importlist

if((Test-Path $errorPath)){

$output_error = get-content $errorPath | sort | get-unique

Out-File -FilePath $errorPath -InputObject $output_error

}

備份還原部分

?1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86#創建文件夾

function CreateFolder($spList, $folderURL)

{

[Microsoft.SharePoint.SPFolder] $spFolder =
$web.GetFolder(($list.RootFolder.Url,$folderURL -Join "/"))

if(!$spFolder.Exists)

{

if (!$list.EnableFolderCreation)

{

$list.EnableFolderCreation = true;

$list.Update();

}

$folders = $folderURL.Trim('/').Split('/');

$folderPath =""

foreach ($f in $folders)

{

$folderPath += "/" + $f;

$folder = $list.ParentWeb.GetFolder($list.RootFolder.Url +
$folderPath);

if (!$folder.Exists)

{

[Microsoft.SharePoint.SPListItem] $newFolder = $list.Items.Add("",
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,
$folderPath.Trim('/'));

$newFolder.Update()

$newFolder["_ModerationStatus"] = 0

$newFolder.Update()

}

}

}

}

#上傳文件

function UploadFile($Web, $DocLibName, $FilePath, $folderUrl)

{

$Web.AllowUnsafeUpdates = $true;

$List = $web.Lists[$DocLibName]

$RootFolder = $List.RootFolder

$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

$File= Get-ChildItem $FilePath

$pageUrl = ( $RootFolder.Url + $folderUrl.TrimEnd("/") +"/"+
$File.Name)

try

{

if(!$Web.GetFile($pageUrl).Exists)

{

[Microsoft.SharePoint.SPFolder] $spFolder = $web.GetFolder( $RootFolder.Url
+ $folderUrl)

$fileStream = ([System.IO.FileInfo] (Get-Item
$File.FullName)).OpenRead()

write-host "上傳文件 " $File.Name " 到 " $RootFolder.Url + $folderUrl "..."

[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($pageUrl,
[System.IO.Stream]$fileStream, $true)

write-host "上傳成功" -ForegroundColor green

write-host

$fileStream.Close()

$spFile.Item.Update()

$spFile.CheckIn("")

$spFile.Approve("")

}

}

catch

{

write-host $_.exception -ForegroundColor red

}

finally

{

$Web.AllowUnsafeUpdates = $false;

}

}

$File = "d:\zhangyuepeng\backup\importlist.txt"

Get-Content $File | foreach-Object {

$items = $_.Split(',')

$Url = $items[0]

$regResult = GetMatches($Url)

$webUrl = $regResult.web

$web = Get-SPWeb $webUrl

$list = $web.Lists["頁面"]

if($items[2] -ne "/")

{

CreateFolder $list $items[2]

UploadFile $web "頁面" $items[1] $items[2] $items[2]

}

else

{

UploadFile $web "頁面" $items[1] "" $items[2]

}

$web.dispose()

}

G. Android上傳文件的方法有哪些HTTPost可以上傳文件嗎

可以上傳 單數傳輸文件可以用ftp實現 你服務端開通一個ftp 指定存放的目錄然後寫個客戶端連接 上傳到指定的目錄後返迴文件名稱 保存就好了

H. android怎麼同時上傳文件和數據

Part[] parts;
文字
parts[i++] = new StringPart(key, value, HTTP.UTF_8);
附件
// parts[i++] = new FilePart(file.getKey(), file.getValue());
// parts[i++] = new FilePart(file.getKey(),
// file.getValue().getName(),
// file.getValue(), null, HTTP.UTF_8);
parts[i++] = new FilePart(file.getKey(), file.getValue().getName(),file.getValue());
上傳
httpPost.setEntity(new MultipartEntity(parts, httpPost.getParams()));
去下載開源的StringPart FilePart MultipartEntity

I. 移動開發中,遇到了安卓不能支持HTML5文件上傳的問題,怎麼解決

  • PC端上傳文件多半用插件,引入flash都沒關系,但是移動端要是還用各種冗餘的插件估計得被噴死,項目裡面需要做圖片上傳的功能,既然H5已經有相關的介面且兼容性良好,當然優先考慮用H5來實現。

  • 用的技術主要是:

  1. ajax;

  2. FileReader;

  3. FormData;

  • HTML結構:

熱點內容
三星錄音文件夾 發布:2024-10-23 02:05:10 瀏覽:828
觸摸屏反編譯 發布:2024-10-23 02:04:22 瀏覽:884
androidsdk22下載 發布:2024-10-23 01:58:22 瀏覽:181
蘋果單職業超變傳奇安卓版叫什麼 發布:2024-10-23 01:54:33 瀏覽:92
我要養雞源碼 發布:2024-10-23 01:54:22 瀏覽:501
android接收消息 發布:2024-10-23 01:52:58 瀏覽:923
飢荒搭建伺服器有寶箱嗎 發布:2024-10-23 01:42:08 瀏覽:589
五指速演算法 發布:2024-10-23 01:41:58 瀏覽:116
360修復藍屏上傳失敗 發布:2024-10-23 01:33:01 瀏覽:921
c語言二級程序設計題 發布:2024-10-23 01:17:52 瀏覽:497