編輯:關於Android編程
本文要演示的是使用Android程序作為客戶端(使用HttpURLConnection訪問網絡),Golang程序作為服務器端,實現文件上傳。
客戶端代碼:
public static String uploadFile(String uploadUrl, String filePath) {
Log.v(TAG, "url:" + uploadUrl);
Log.v(TAG, "filePath:" + filePath);
String nextLine = "\r\n";
String dividerStart = "--";
String boundary = "******";
try {
URL url = new URL(uploadUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setChunkedStreamingMode(1024 * 256);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
// 設置Http請求頭
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
//必須在Content-Type 請求頭中指定分界符
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
//定義數據寫入流,准備上傳文件
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(dividerStart + boundary + nextLine);
//設置與上傳文件相關的信息
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ filePath.substring(filePath.lastIndexOf("/") + 1) + "\"" + nextLine);
dos.writeBytes(nextLine);
FileInputStream fis = new FileInputStream(filePath);
byte[] buffer = new byte[1024 * 32];
int count;
// 讀取文件內容,並寫入OutputStream對象
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(nextLine);
dos.writeBytes(dividerStart + boundary + dividerStart + nextLine);
dos.flush();
// 開始讀取從服務器傳過來的信息
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String result = br.readLine();
dos.close();
is.close();
connection.disconnect();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
package webserver
//接收客戶端通過http上傳的文件
//Date: 2015-3-25 16:18:33
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
func UpLoadBase() {
fmt.Println("This is uploadbase")
http.HandleFunc("/httpUploadFile", handleUploadFile)
http.ListenAndServe(":8086", nil)
if err != nil {
fmt.Println("ListenAndServe error: ", err.Error())
}
}
func handleUploadFile(w http.ResponseWriter, r *http.Request) {
fmt.Println("client:", r.RemoteAddr)
file, fileHeader, err := r.FormFile("file")
if err != nil {
log.Fatal("FormFile:", err.Error())
return
}
defer func() {
if err := file.Close(); err != nil {
log.Fatal("Close:", err.Error())
return
}
}()
//文件名
fileName := fileHeader.Filename
if fileName == "" {
log.Fatal("Param filename cannot be null.")
return
}
//文件內容
bytes, err := ioutil.ReadAll(file)
//寫到服務端本地文件中
outputFilePath := "/home/admin/桌面/" + fileName
err = ioutil.WriteFile(outputFilePath, bytes, os.ModePerm)
if err != nil {
log.Fatal("WriteFileError:", err.Error())
return
}
w.Write(([]byte)("上傳文件成功!"))
}
懸浮對話框Android代碼實現
直接上代碼: private void setDialog(){ View view = getLayoutInflater().inflate(R.
Android編程滑動效果之Gallery仿圖像集浏覽實現方法
本文實例講述了Android編程滑動效果之Gallery仿圖像集浏覽實現方法。分享給大家供大家參考,具體如下:Android系統自帶一個Gallery浏覽圖片的應用,通過
Android Drawable必備知識小結
什麼是Drawable首先Drawable是一個抽象類,表示的是可以在Canvas中繪制的圖像,常被用作一個view的背景,有多種實現類完成不同的功能。其次Drawabl
android開發之使用SQLite數據庫(db文件)
在開發中,有時需要使用db文件數據庫,所以就需要將其導入項目,再將其使用程序寫入到應用的db文件下使用。 代碼很簡單,可以拿來直接使用。 要使用需要兩個步驟: 1.創建r