利用HttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法
更新時(shí)間:2016年11月19日 10:21:37 投稿:jingxian
下面小編就為大家?guī)?lái)一篇利用HttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
如下所示:
//客戶端代碼 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(); // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行 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(); } } } //服務(wù)端代碼 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); } //響應(yīng)信息 json字符串格式 Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put("flag", true); //生成響應(yīng)的json字符串 String jsonResponse = JSONObject.toJSONString(responseMap); sendResponse(jsonResponse); } catch (IOException e) { //響應(yīng)信息 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; } /** * 返回響應(yīng) * * @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 上傳 接收文件的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 談?wù)凧ava利用原始HttpURLConnection發(fā)送POST數(shù)據(jù)
- java后臺(tái)調(diào)用HttpURLConnection類模擬瀏覽器請(qǐng)求實(shí)例(可用于接口調(diào)用)
- Android HttpURLConnection.getResponseCode()錯(cuò)誤解決方法
- Java HttpURLConnection超時(shí)和IO異常處理
- Android 中HttpURLConnection與HttpClient使用的簡(jiǎn)單實(shí)例
- Android中HttpURLConnection與HttpClient的使用與封裝
- Android中使用HttpURLConnection實(shí)現(xiàn)GET POST JSON數(shù)據(jù)與下載圖片
- Android程序開(kāi)發(fā)通過(guò)HttpURLConnection上傳文件到服務(wù)器
- Android通過(guò)HttpURLConnection和HttpClient接口實(shí)現(xiàn)網(wǎng)絡(luò)編程
- Android網(wǎng)絡(luò)技術(shù)HttpURLConnection詳解
相關(guān)文章
springboot 返回json格式數(shù)據(jù)時(shí)間格式配置方式
這篇文章主要介紹了springboot 返回json格式數(shù)據(jù)時(shí)間格式配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Mybatis游標(biāo)查詢大量數(shù)據(jù)方式
這篇文章主要介紹了Mybatis游標(biāo)查詢大量數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Spring Boot JPA中java 8 的應(yīng)用實(shí)例
這篇文章主要介紹了Spring Boot JPA中java 8 的應(yīng)用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02基于logback實(shí)現(xiàn)純java版本的SDK組件
這篇文章主要介紹了基于logback實(shí)現(xiàn)純java版本的SDK組件,在項(xiàng)目開(kāi)發(fā)過(guò)程中通常會(huì)使用logback作為日志記錄的依賴工具,使用方式是引入logback相關(guān)jar包,然后配置logback.xml配置文件的方式來(lái)實(shí)現(xiàn),需要的朋友可以參考下2023-11-11