如下所示 :
// 客户端代码 public static void main (String [] args) löst ioException {datainputStream in = null aus; OutputStream out = null; HttpurlConnection conn = null; JsonObject resposetxt = null; InputStream INS = NULL; BytearrayoutputStream outstream = null; try {url url = new url ("http://10.28.160.160:9080/main/uploadFile?filename= 列表 .txt"); conn = (httpurlConnection) url.openconnection (); // 发送 post 请求必须设置如下两行 conn.setDooutput (true); Conn.SetUSecaches (falsch); Conn.SetRequestMethod ("Post"); Conn.SetRequestProperty ("Inhaltstyp", "Text/HTML"); Conn.SetRequestProperty ("Cache-Control", "No-Cache"); Conn.SetRequestProperty ("CharSert", "UTF-8"); Conn.Connect (); Conn.SetConnectTimeout (10000); out = conn.getOutputStream (); Datei Datei = new Datei ("h: /users/chengtingyu/desktop/test/list.txt"); in = new DataNputStream (neuer FileInputStream (Datei)); int bytes = 0; byte [] buffer = neues byte [1024]; while ((bytes = in.read (puffer))! = -1) {out.write (puffer, 0, bytes); } out.flush (); // 返回流 if (conn.getResponSCode () == httpurlConnection.http_ok) {iNS = conn.getInputStream (); outstream = new bytearrayoutputStream (); byte [] data = new Byte [1024]; int count = -1; while ((count = Ins.Read (Daten, 0, 1024))! = -1) {outstream.write (Daten, 0, count); } data = null; resposetxt = jsonObject.parseObject (neuer String (überstream .Tobytearray (), "utf-8"); }} catch (Ausnahme e) {e.printstacktrace (); } endlich {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öst eine Ausnahme aus {String Dateiname = Request.getParameter ("Dateiname"); String FilefullPath = "H:/user/chengtingyu/desktop/" + Dateiname; InputStream input = null; FileOutputStream fos = null; try {input = request.getInputStream (); Datei Datei = neue Datei ("H:/user/chengtingyu/Desktop"); if (! file.exists ()) {file.mkdirs (); } fos = new FileOutputStream (DateifullPath); int size = 0; byte [] buffer = neues byte [1024]; while ((size = input.read (puffer, 0,1024))! = -1) {fos.write (puffer, 0, size); } // 响应信息 json 字符串格式 map <string, object> respotsemap = new HashMap <String, Object> (); ResponseMap.put ("Flag", wahr); // 生成响应的 JSON 字符串 String jsonResponse = jsonObject.tojonstring (reagemap); SendResponse (JSONResponse); } catch (ioException e) {// 响应信息 json 字符串格式 map <string, Object> respotsemap = new HashMap <String, Object> (); ResponseMap.put ("Flag", False); ResponseMap.put ("errormsg", e.getMessage ()); String jsonResponse = jsonObject.tojonstring (responseMap); SendResponse (JSONResponse); } endlich {if (input! = null) {input.close (); } if (fos! = null) {fos.close (); }} return null; }/** * 返回响应 * * @throws Exception */private void sendResponse (String-Antwort) löst die Ausnahme aus {response.setContentType ("application/json; charSet = utf-8"); Printwriter PW = NULL; try {pw = response.getWriter (); pw.write (responstring); pw.flush (); } endlich {ioutils.closequiet (pw); }}以上这篇利用 httpurlConnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。