如下所示 :
// 客户端代码 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; try {url url = new url ("http://10.28.160.160:9080/main/uploadfile?filename= 列表 .txt"); conn = (httpurlconnection) url.openconcenection (); // 发送 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"); in = new DataInputStream (new FileInputStream (file)); int bytes = 0; Byte [] buffer = новый байт [1024]; while ((bytes = in.read (buffer))! = -1) {out.write (буфер, 0, байты); } out.flush (); // 返回流 if (conn.getResponsecode () == httpurlConnection.http_ok) {ins = conn.getInputStream (); outstream = new BytearrayOutputStream (); byte [] data = new Byte [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 (); } наконец {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 () Throws 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/desktop"); if (! file.exists ()) {file.mkdirs (); } fos = new FileOutputStream (filefullPath); int size = 0; Byte [] buffer = новый байт [1024]; while ((size = input.read (буфер, 0,1024))! = -1) {fos.write (буфер, 0, размер); } // 响应信息 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 (); }} return null; }/** * 返回响应 * * @Throhs Exception */private void SendResponse (string responseString) throws exection {response.setContentType ("Application/json; charset = utf-8"); Printwriter pw = null; try {pw = response.getWriter (); PW.Write (отвечает); pw.flush (); } наконец {ioutils.closequietly (pw); }}以上这篇利用 httpurlConnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。 也希望大家多多支持武林网。