: :
// public public static void main (string [] args) يلقي ioException {datainputStream in = null ؛ OutputStream Out = null ؛ httpurlConnection conn = null ؛ jsonobject resposetxt = null ؛ inputStream ins = null ؛ ByteArrayoutputStream OutStream = NULL ؛ جرب {url url = url new url ("http://10.28.160.160:9080/main/uploadfile؟filename= 列表 .txt") ؛ conn = (httpurlConnection) url.openconnection () ؛ // 发送 post 请求必须设置如下两行 conn.setDooutput (true) ؛ conn.setuseCaches (false) ؛ Conn.SetRequestMethod ("post") ؛ Conn.SetRequestProperty ("نوع المحتوى" ، "Text/HTML") ؛ Conn.SetRequestProperty ("Cache-Control" ، "no-cache") ؛ Conn.SetRequestProperty ("charsert" ، "UTF-8") ؛ conn.connect () ؛ Conn.SetConnectTimeout (10000) ؛ out = conn.getOutputStream () ؛ ملف ملف = ملف جديد ("H: /users/Chengtingyu/Desktop/test/list.txt") ؛ في = جديد datainputStream (FileInputStream جديد) ؛ البايت int = 0 ؛ Byte [] Buffer = New Byte [1024] ؛ بينما ((bytes = in.read (buffer))! = -1) {out.write (buffer ، 0 ، bytes) ؛ } out.flush () ؛ // 返回流 if (conn.getResPonsecode () == httpurlconnection.http_ok) {ins = conn.getInputStream () ؛ OutStream = new ByTearRayOutputStream () ؛ Byte [] Data = New Byte [1024] ؛ عدد int = -1 ؛ بينما ((count = ins.read (البيانات ، 0 ، 1024))! = -1) {overstream.write (البيانات ، 0 ، العد) ؛ } data = null ؛ resposetxt = jsonobject.parseObject (سلسلة جديدة (OutStream .TobyTearRay () ، "UTF-8")) ؛ }} catch (استثناء e) {E.PrintStackTrace () ؛ } أخيرًا {if (in! = null) {in.close () ؛ } if (out! = null) {out.close () ؛ } if (ins! = null) {ins.close () ؛ } if (overstream! = null) {OutStream.Close () ؛ } if (conn! = null) {conn.Disconnect () ؛ }}} // 服务端代码 السلسلة العامة uploadFile () يلقي استثناء {String filename = request.getParameter ("filename") ؛ String FileFullPath = "H:/Users/Chengtingyu/Desktop/" + filename ؛ إدخال inputStream = فارغ ؛ fileOutputStream fos = null ؛ حاول {input = request.getInputStream () ؛ ملف ملف = ملف جديد ("H:/Users/Chengtingyu/Desktop") ؛ if (! file.exists ()) {file.mkdirs () ؛ } fos = fileOutputStream جديد (filefullPath) ؛ حجم int = 0 ؛ Byte [] Buffer = New Byte [1024] ؛ بينما ((size = input.Read (buffer ، 0،1024))! = -1) {fos.write (buffer ، 0 ، size) ؛ } // 响应信息 json 字符串格式 map <string ، object> responseMap = new hashMap <string ، Object> () ؛ ResponseMap.put ("Flag" ، true) ؛ // 生成响应的 json 字符串 string jsonresponse = jsonobject.tojsonstring (responseMap) ؛ SendResponse (jsonresponse) ؛ } catch (ioException e) {// 响应信息 json 字符串格式 map <string ، object> responseMap = new hashmap <string ، object> () ؛ ResponseMap.put ("Flag" ، false) ؛ ResponseMap.put ("errormsg" ، e.getMessage ()) ؛ String jsonResponse = jsonobject.tojsonstring (responseMap) ؛ SendResponse (jsonresponse) ؛ } أخيرًا {if (input! = null) {input.close () ؛ } if (fos! = null) {fos.close () ؛ }} الإرجاع null ؛ }/** * 返回响应 * * athrows استثناء */private void sendResponse (سلسلة استجابة) يلقي الاستثناء {response.setContentType ("application/json ؛ charset = utf-8") ؛ printWriter pw = null ؛ جرب {pw = reponse.getWriter () ؛ pw.write (الاستجابة) ؛ pw.flush () ؛ } أخيرًا {ioutils.closequietly (pw) ؛ }}以上这篇利用 httpurlconnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。