Golang+Android基于HttpURLConnection实现的文件上传功能示例

Golang+Android基于HttpURLConnection实现的文件上传功能示例,第1张

概述本文实例讲述了Golang+Android基于HttpURLConnection实现的文件上传功能。分享给大家供大家参考,具体如下:

本文实例讲述了Golang+AndroID基于httpURLConnection实现的文件上传功能。分享给大家供大家参考,具体如下:

这里要演示的是使用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,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;}

服务器端代码:@H_301_11@复制代码 代码如下:package webserver@H_301_11@//接收客户端通过http上传的文件@H_301_11@//Date: 2015-3-25 16:18:33@H_301_11@import (@H_301_11@    "fmt"@H_301_11@    "io/IoUtil"@H_301_11@    "log"@H_301_11@    "net/http"@H_301_11@    "os"@H_301_11@)@H_301_11@func UpLoadBase() {@H_301_11@    fmt.Println("This is uploadbase")@H_301_11@    http.HandleFunc("/httpUploadfile",handleUploadfile)@H_301_11@    http.ListenAndServe(":8086",nil)@H_301_11@    if err != nil {@H_301_11@        fmt.Println("ListenAndServe error: ",err.Error())@H_301_11@    }@H_301_11@}@H_301_11@func handleUploadfile(w http.ResponseWriter,r *http.Request) {@H_301_11@    fmt.Println("clIEnt:",r.RemoteAddr)@H_301_11@    file,fileheader,err := r.Formfile("file")@H_301_11@    if err != nil {@H_301_11@        log.Fatal("Formfile:",err.Error())@H_301_11@        return@H_301_11@    }@H_301_11@    defer func() {@H_301_11@        if err := file.Close(); err != nil {@H_301_11@            log.Fatal("Close:",err.Error())@H_301_11@            return@H_301_11@        }@H_301_11@    }()@H_301_11@    //文件名@H_301_11@    filename := fileheader.filename@H_301_11@    if filename == "" {@H_301_11@        log.Fatal("Param filename cannot be null.")@H_301_11@        return@H_301_11@    }@H_301_11@    //文件内容@H_301_11@    bytes,err := IoUtil.ReadAll(file)@H_301_11@    //写到服务端本地文件中@H_301_11@    outputfilePath := "/home/admin/桌面/" + filename@H_301_11@    err = IoUtil.Writefile(outputfilePath,bytes,os.ModePerm)@H_301_11@    if err != nil {@H_301_11@        log.Fatal("WritefileError:",err.Error())@H_301_11@        return@H_301_11@    }@H_301_11@    w.Write(([]byte)("上传文件成功!"))@H_301_11@}

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Golang+Android基于HttpURLConnection实现的文件上传功能示例全部内容,希望文章能够帮你解决Golang+Android基于HttpURLConnection实现的文件上传功能示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1146879.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存