/** * 調用流程上傳文件接口上傳文件 * @param path * @return */ public String sendPostUplodFile(String path) { DataOutputStream out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(uploadPic + "?caseId=klxxxxxx"); //打開和URL之間的連接 HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection(); //發送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); String BOUNDARY = "----WebKitFormBoundary07I8UIuBx6LN2KyY"; conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); conn.connect(); out = new DataOutputStream(conn.getOutputStream()); byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); //添加參數 StringBuffer sb1 = new StringBuffer(); sb1.append("--"); sb1.append(BOUNDARY); sb1.append("\r\n"); sb1.append("Content-Disposition: form-data;name=\"luid\""); sb1.append("\r\n"); sb1.append("\r\n"); sb1.append("123"); sb1.append("\r\n"); out.write(sb1.toString().getBytes()); //添加參數file File file = new File(path); StringBuffer sb = new StringBuffer(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\""); sb.append("\r\n"); sb.append("Content-Type: application/octet-stream"); sb.append("\r\n"); sb.append("\r\n"); out.write(sb.toString().getBytes()); DataInputStream in1 = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in1.read(bufferOut)) != -1) { out.write(bufferOut,0,bytes); } out.write("\r\n".getBytes()); in1.close(); out.write(end_data); //flush輸出流的緩沖 out.flush(); //定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { e.printStackTrace(); }finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (Exception ex) { ex.printStackTrace(); } } return result; }
/** * 文件上傳保存接口 * @param request * @param caseId * @return * @throws IOException */ public Json upload(HttpServletRequest request, String caseId) throws IOException { String oper = "/file/upload"; if (StringUtils.isEmpty(caseId)) { return Json.fail(oper, "參數錯誤"); } MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest) request; //得到文件map對象 Map<String, MultipartFile> files = Murequest.getFileMap(); System.out.println("files ========" + files.toString()); List<TabCaseMaterial> list = new ArrayList<>(); for (MultipartFile file : files.values()) { file.getSize(); //獲取文件名 String fileName = file.getOriginalFilename(); System.out.println("上傳的文件名為:" + fileName); // 獲取文件的后綴名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); String newfileName = UUID.randomUUID().toString() + suffixName; //完全替換文件名稱 2020-07-17 System.out.println("newfileName =" + newfileName); StringBuilder sb = new StringBuilder(newfileName); //創建文件對象 File tagetFile = new File(physicpath + File.separator + caseId + File.separator + sb.toString()); // 如果上傳文件夾不存在則創建 if (!tagetFile.getParentFile().exists() && !tagetFile.isDirectory()) { tagetFile.getParentFile().mkdirs(); tagetFile.createNewFile(); } else if (!tagetFile.exists()) { tagetFile.createNewFile(); } try { file.transferTo(tagetFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String backUrl = ""; backUrl = tomcatpath + "/" + caseId + "/" + newfileName; System.out.println("backUrl ==== " + backUrl); TabCaseMaterial material = new TabCaseMaterial(); material.setOriName(fileName.substring(0, fileName.lastIndexOf("."))); material.setFileName(newfileName); material.setFileUrl(backUrl); list.add(material); } return Json.succ(oper, list); }