如下所示 :
// 客户端代码 public static void main (string [] args) melempar ioException {datalputStream in = null; OutputStream out = null; HttpurlConnection conn = null; JsonObject resposetxt = null; InputStream INS = NULL; BytearrayoutputStream outstream = null; coba {url url = url baru ("http://10.28.160.160:9080/main/uploadfile?filename= 列表 .txt"); conn = (httpurlConnection) url.openconnection (); // 发送 Posting 请求必须设置如下两行 Conn.SetDoOutput (true); Conn.setusecaches (false); Conn.setRequestMethod ("Post"); conn.setRequestProperty ("tipe konten", "teks/html"); Conn.setRequestProperty ("Cache-Control", "No-Cache"); Conn.setRequestProperty ("Charsert", "UTF-8"); conn.connect (); Conn.SetConnectTimeout (10000); out = conn.getoutputStream (); File file = file baru ("h: /users/chengtingyu/desktop/test/list.txt"); di = DataInputStream baru (FileInputStream baru (file)); int bytes = 0; byte [] buffer = byte baru [1024]; while ((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 = byte baru [1024]; int count = -1; while ((count = ins.read (data, 0, 1024))! = -1) {outstream.write (data, 0, count); } data = null; respoSetxt = jsonobject.parseObject (string baru (outstream .tObyTeArray (), "UTF-8")); }} catch (Exception e) {e.printstacktrace (); } akhirnya {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 unggahfile () melempar Exception {string filename = request.getParameter ("FileName"); String filefullpath = "h:/user/chengtingyu/desktop/" + nama file; Inputstream input = null; FileOutputStream fos = null; coba {input = request.getInputStream (); File file = file baru ("h:/user/chengtingyu/desktop"); if (! file.exists ()) {file.mkdirs (); } fos = FileOutputStream baru (FilefullPath); ukuran int = 0; byte [] buffer = byte baru [1024]; while (((size = input.read (buffer, 0,1024))! = -1) {fos.write (buffer, 0, size); } // 响应信息 JSON 字符串格式 Peta <String, Object> ResponseMap = HashMap baru <String, Object> (); responseMap.put ("flag", true); // 生成响应的 json 字符串 string jsonResponse = jsonobject.toJsonstring (responseMap); SendResponse (JsonResponse); } catch (ioException e) {// 响应信息 json 字符串格式 peta <string, object> responseMap = new HashMap <String, Object> (); responseMap.put ("flag", false); responseMap.put ("errorMsg", e.getMessage ()); String jsonResponse = jsonObject.toJsonstring (responseMap); SendResponse (JsonResponse); } akhirnya {if (input! = null) {input.close (); } if (fos! = null) {fos.close (); }} return null; }/** * 返回响应 * * @throws Exception */private void sendResponse (string responseString) melempar Exception {response.setContentType ("Application/json; charset = UTF-8"); Printwriter pw = null; coba {pw = response.getWriter (); pw.write (responseString); pw.flush (); } akhirnya {ioutils.closequietly (pw); }}以上这篇利用 httpurlconnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。