如下所示 :
// public static void main (String [] args)은 ioexception {datainputstream in = null; outputStream out = null; httpurlconnection conn = null; jsonobject resposetxt = null; 입력 스트림 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.setUeCaches (False); conn.setRequestMethod ( "post"); conn.setRequestProperty ( "content-type", "text/html"); Conn.setRequestProperty ( "캐시-제어", "No-Cache"); Conn.setRequestProperty ( "Charsert", "UTF-8"); conn.connect (); Conn.setConnectTimeout (10000); out = conn.getoutputStream (); 파일 = 새 파일 ( "h : /users/chengtingyu/desktop/test/list.txt"); in = new DatainputStream (new FileInputStream (file)); int 바이트 = 0; 바이트 [] 버퍼 = 새로운 바이트 [1024]; while ((bytes = in.read (buffer))! = -1) {out.write (buffer, 0, bytes); } out.flush (); // if (conn.getResponseCode () == httpurlConnection.http_ok) {ins = conn.getInputStream (); 아웃 스트림 = 새로운 BytearRayoutputStream (); 바이트 [] 데이터 = 새로운 바이트 [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 (예외 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 ()는 예외를 {String filename = request.getParameter ( "filename"); String filefullpath = "h :/user/chengtingyu/goodtop/" + filename; inputStream input = null; fileoutputStream fos = null; try {input = request.getInputStream (); 파일 = 새 파일 ( "h :/user/chengtingyu/goodtop"); if (! file.exists ()) {file.mkdirs (); } fos = 새 FileOutputStream (FileFullPath); int size = 0; 바이트 [] 버퍼 = 새로운 바이트 [1024]; while ((size = input.read (버퍼, 0,1024))! = -1) {fos.write (버퍼, 0, size); } // 响应信息 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 ()); 문자열 jsonResponse = jsonobject.tojsonstring (responsemap); SendResponse (jsonResponse); } 마지막으로 {if (input! = null) {input.close (); } if (fos! = null) {fos.close (); }} return null; }/** * * * @Throws Exception */private void sendResponse (String responseString) 예외 {response.setContentType ( "application/json; charset = utf-8"); 인쇄기 PW = NULL; try {pw = response.getwriter (); pw.write (응답); pw.flush (); } 마침내 {ioutils.closequietly (pw); }}以上这篇利用 httpurlconnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 接收文件的实现方法就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持武林网。 也希望大家多多支持武林网。