:
// 客户端代码 public static void main (string [] args) lève ioException {dataRainputStream dans = null; OutputStream out = null; Httpurlconnection conn = null; JSONObject RespOsetxt = null; InputStream ins = null; BytearrayoutputStream outstream = null; essayez {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 ("Content-Type", "Text / Html"); Conn.SetRequestProperty ("Cache-Control", "No-Cache"); Conn.SetRequestProperty ("Charsert", "UTF-8"); Conn.Connect (); Conn.SetConnectTimeout (10000); out = conn.getOutputStream (); File file = new File ("h: /users/chengtingyu/desktop/test/list.txt"); dans = new DatAnputStream (new FileInputStream (fichier)); int octets = 0; octet [] tampon = nouveau octet [1024]; while ((bytes = in.read (tampon))! = -1) {out.write (tampon, 0, octets); } out.flush (); // 返回流 if (conn.getResponSECODE () == httpurlconnection.http_ok) {ins = conn.getInputStream (); outstream = new bytearrayoutputStream (); octet [] data = nouveau octet [1024]; int count = -1; while ((count = ins.read (data, 0, 1024))! = -1) {outstream.write (data, 0, count); } data = null; resposetxt = jsonObject.ParseObject (new String (outStream .ToByTearray (), "UTF-8")); }} catch (exception e) {e.printStackTrace (); } enfin {if (in! = null) {in.close (); } if (out! = null) {out.close (); } if (ins! = null) {ins.close (); } if (outstream! = null) {outstream.close (); } if (conn! = null) {conn.disconnect (); }}} // 服务端代码 public String uploadFile () lève exception {String filename = request.getParameter ("filename"); String filefullpath = "h: / users / chengtingyu / Desktop /" + filename; InputStream input = null; FileoutputStream fos = null; try {input = request.getInputStream (); File file = new File ("h: / users / chengtingyu / bureau"); if (! file.exists ()) {file.mkDirs (); } fos = new FileOutputStream (filefullPath); int size = 0; octet [] tampon = nouveau octet [1024]; while ((size = input.read (tampon, 0,1024))! = -1) {fos.write (tampon, 0, taille); } // 响应信息 JSON 字符串格式 Map <String, Object> ResponseMap = new HashMap <String, Object> (); ResponseMap.put ("Flag", True); // 生成响应的 JSON 字符串 String JSonResponse = JSONObject.tojSontring (ResponseMap); sendResponse (jsonResponse); } catch (ioException e) {// 响应信息 JSON 字符串格式 Map <String, objet> ResponseMap = new HashMap <String, Object> (); ResponseMap.put ("Flag", false); ResponseMap.put ("errormsg", e.getMessage ()); String JSonResponse = JSONObject.tojSontring (ResponseMap); sendResponse (jsonResponse); } enfin {if (input! = null) {input.close (); } if (fos! = null) {fos.close (); }} return null; } / ** * 返回响应 * * @throws exception * / private void sendResponse (String ResponseString) lève l'exception {réponse.setContentType ("application / json; charset = utf-8"); Printwriter pw = null; essayez {pw = réponse.getWriter (); PW.Write (ResponseString); pw.flush (); } enfin {ioutils.closequietly (pw); }}以上这篇利用 httpurlconnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。