pom 의존성 가져오기
<종속성> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4</version> </dependency> <종속성> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <버전>4.4</version> < /종속성>
업로드 코드:
/** * * @param filepath 文件全路径 * @param fileName 文件名 * @return * @throws IOException */ @PostMapping(value = "/upfileload.do") public String upfileload(String filepath,String fileName) throws IOException { System.out.println("传入参数------》" + 파일 경로 + "----" + 파일 이름); DataInputStream 입력 = null; OutputStream 출력 = null; HttpURLConnection conn = null; JSONObject resposeTxt = null; InputStream ins = null; ByteArrayOutputStream outStream = null; 노력하다 { // URL URL = 새 URL("http://192.168.3.11:8081/mes-boot-doc/test/fileupload?fileName=shafei.xls"); URL url = 새 URL("http://localhost:8009/fileupload?fileName="+fileName); conn = (HttpURLConnection) url.openConnection(); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setUseCaches(거짓); conn.setRequestMethod("POST"); conn.setRequestProperty("콘텐츠 유형", "텍스트/html"); conn.setRequestProperty("캐시 제어", "캐시 없음"); conn.setRequestProperty("문자트", "UTF-8"); 연결 연결하다(); conn.setConnectTimeout(10000); 출력 = conn.getOutputStream(); 파일 파일 = 새 파일(파일 경로); in = new DataInputStream(new FileInputStream(file)); 정수 바이트 = 0; 바이트[] 버퍼 = 새 바이트[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(); 바이트[] 데이터 = 새 바이트[1024]; 정수 수 = -1; while ((count = ins.read(data, 0, 1024)) != -1) { } outStream.write(데이터, 0, 개수); } 데이터 = 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(); outStream.close(); if (outStream != null) { } if (conn != null) { conn.disconnect(); } } "성공"을 반환합니다. }
수신 코드:
@PostMapping("/fileupload") public String uploadFile(HttpServletRequest 요청,HttpServletResponse 응답) throws Exception{ String fileName = request.getParameter("fileName"); System.out.println("파일명:"+파일명); String fileFullPath = "C:/HWKJ/FTP/" + 파일명; InputStream 입력 = null; FileOutputStream fos = null; try { 입력 = request.getInputStream(); 파일 file = new File("C:/HWKJ/FTP/"); if(!file.exists()){ file.mkdirs(); } fos = new FileOutputStream(fileFullPath); 정수 크기 = 0; 바이트[] 버퍼 = 새 바이트[1024]; 동안 ((크기 = input.read(버퍼,0,1024)) ! fos.write(buffer, 0, size); } //응답 정보 json 문자열 형식 Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put("flag", true); // 응답 생성 json string String jsonResponse = JSONObject.toJSONString(responseMap); sendResponse(jsonResponse,response); } catch (IOException e) { //응답 정보 json string format Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put("flag", false); responseMap.put("errorMsg", e.getMessage ()); String jsonResponse = JSONObject.toJSONString(responseMap); sendResponse(jsonResponse,response); } 마지막으로{ if(입력 != null){ input.close(); } if(fos != null){ fos.close(); } } null을 반환합니다. } /** * 返回响应 * * @throws Exception */ private void sendResponse(String responseString,HttpServletResponse response) throws Exception { response.setContentType("application/json;charset=UTF-8"); PrintWriter pw = null; try { pw = response.getWriter(); pw.write(responseString); pw.flush(); } 마침내 { IOUtils.closeQuietly(pw); } }
우편 배달부 테스트