如下所示 :
// 客户端代码 public static void main (string [] args) lanza ioexception {datainputStream in = null; OutputStream out = null; Httpurlconnection conn = null; JsonObject Resposetxt = NULL; InputStream INS = NULL; ByteArRayOutputStream Outstream = null; Pruebe {url url = nuevo url ("http://10.28.160.160:9080/main/uploadfile?fileName= 列表 .txt"); conn = (httpurlconnection) url.openconnection (); // 发送 Post 请求必须设置如下两行 Conn.setDoOutput (verdadero); conn.setUsecaches (falso); 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 (); Archivo archivo = nuevo archivo ("h: /users/chengtyu/desktop/test/list.txt"); in = nuevo DataInputStream (nuevo FileInputStream (archivo)); int bytes = 0; byte [] buffer = new Byte [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 byteArRaReOutputStream (); 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 (); } finalmente {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 () lanza la excepción {String filename = request.getParameter ("FileName"); Cadena filefullPath = "h:/users/chengtingyu/escritorio/" + nombre de archivo; InputStream Input = NULL; FileOutputStream fos = null; intente {input = request.getInputStream (); Archivo archivo = nuevo archivo ("h:/users/chengtingyu/escritorio"); if (! file.exists ()) {file.mkdirs (); } fos = new FileOutputStream (FileFullPath); int tamaño = 0; byte [] buffer = new Byte [1024]; while ((size = input.read (buffer, 0,1024))! = -1) {fos.write (buffer, 0, size); } // 响应信息 JSON 字符串格式 MAP <String, Object> ResponseMap = new HashMap <String, Object> (); RespuestaMap.put ("Flag", True); // 生成响应的 json 字符串 string jsonResponse = jsonObject.ToJSonstring (RespuestaMap); sendResponse (jsonResponse); } catch (ioException e) {// 响应信息 json 字符串格式 map <string, object> resphipeMap = new HashMap <String, Object> (); RespuestaMap.put ("Flag", False); ResponseMap.put ("Errormsg", E.getMessage ()); Cadena jsonResponse = jsonObject.ToJSonstring (RespuestaMap); sendResponse (jsonResponse); } finalmente {if (input! = null) {input.close (); } if (fos! = null) {fos.close (); }} return null; }/** * 返回响应 * * @throws Exception */private void sendResponse (String ResponseString) lanza la excepción {Response.SetContentType ("Application/Json; Charset = UTF-8"); PrintWriter pw = null; intente {pw = respuesta.getwriter (); pw.write (Responsestring); pw.flush (); } finalmente {ioutils.closequietty (pw); }}以上这篇利用 httpurlconnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 , , 也希望大家多多支持武林网。