如下所示
// 客户端代码 public static void main (string [] args) lança IoException {DatainputStream em = null; OutputStream out = null; HttpurlConnection Conn = null; JsonObject Resposetxt = null; InputStream ins = null; ByteArrayOutputStream Outream = null; tente {url url = new url ("http://10.28.160.160:9080/main/uploadfile?filename= 列表 .txt"); conn = (httpurlConnection) url.openconnection (); // 发送 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 (); Arquivo de arquivo = novo arquivo ("h: /users/chengtingyu/desktop/test/list.txt"); in = new DatainputStream (new FileInputStream (FILE)); int bytes = 0; byte [] buffer = novo byte [1024]; while ((bytes = in.read (buffer))! = -1) {out.write (buffer, 0, bytes); } out.flush (); // 返回流 if (conn.getResponsecode () == httpurlconnection.http_ok) {ins = conn.getInputStream (); Outream = new ByteArrayOutputStream (); byte [] dados = novo byte [1024]; int count = -1; while ((count = ins.read (dados, 0, 1024))! = -1) {ostream.Write (dados, 0, contagem); } data = null; Resposetxt = jsonObject.ParSeObject (new String (Outream .TobyTearray (), "UTF-8")); }} catch (Exceção e) {e.printStackTrace (); } finalmente {if (in! = null) {in.close (); } if (out! = null) {out.close (); } if (ins! = null) {ins.close (); } if (extream! = null) {Outream.close (); } if (conn! = null) {Conn.Disconnect (); }}} // 服务端代码 public String uPLOPFILE () lança exceção {string filename = request.getParameter ("nome do arquivo"); String filefullpath = "h:/usuários/chengtingyu/desktop/" + nome do arquivo; InputStream input = null; FileOutputStream fos = null; tente {input = request.getInputStream (); Arquivo de arquivo = novo arquivo ("h:/usuários/chengtingyu/desktop"); if (! file.exists ()) {file.mkdirs (); } fos = new FileOutputStream (filefullPath); int size = 0; byte [] buffer = novo byte [1024]; while ((size = input.read (buffer, 0,1024))! = -1) {fos.write (buffer, 0, tamanho); } // 响应信息 json 字符串格式 map <string, object> Responsemap = new Hashmap <string, object> (); responseMap.put ("sinalizador", true); // 生成响应的 json 字符串 string jsonResponse = jsonObject.TojSonstring (Responsemap); SendResponse (JSONResponse); } catch (ioexception e) {// 响应信息 json 字符串格式 map <string, object> Responsemap = new Hashmap <string, object> (); responseMap.put ("sinalizador", false); Responsemap.put ("errorMsg", e.getMessage ()); String jsonResponse = jsonObject.TojSonstring (Responsemap); SendResponse (JSONResponse); } finalmente {if (input! = null) {input.close (); } if (fos! = null) {fos.close (); }} retornar nulo; }/** * 返回响应 * * @Throws Exception */private void sendResponse (String ResponsecSring) lança Exceção {Response.SetContentType ("Application/json; charset = utf-8"); PrintWriter pw = null; tente {pw = Response.getWriter (); pw.write (responseString); pw.flush (); } finalmente {ioutils.closeQuietly (pw); }}以上这篇利用 httpurlConnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。