利用HttpUrlConnection 上传 接收文件的实现方法
程序员文章站
2024-03-12 09:34:20
如下所示:
//客户端代码
public static void main(string[] args) throws ioexception {
da...
如下所示:
//客户端代码 public static void main(string[] args) throws 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.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(); file file = new file("h:/users/chengtingyu/desktop/test/list.txt"); in = new datainputstream(new fileinputstream(file)); 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 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(); } finally { 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 = 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>(); 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); } finally{ if(input != null){ input.close(); } if(fos != null){ fos.close(); } } return null; } /** * 返回响应 * * @throws exception */ private void sendresponse(string responsestring) throws exception { response.setcontenttype("application/json;charset=utf-8"); printwriter pw = null; try { pw = response.getwriter(); pw.write(responsestring); pw.flush(); } finally { ioutils.closequietly(pw); } }
以上这篇利用httpurlconnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。